Use concepts with std::remove_cvref_t -- Sandor Dargo
Let’s talk about templates, constraints, and concepts. We’ll start with a quick reminder of why concepts are essential when working with templates. Then we’ll dive into the challenge posed by reference-qualified types and finish with a practical solution.
Use concepts with std::remove_cvref_t
by Sandor Dargo
From the article:
By now, it’s well known that using unconstrained templates is discouraged. Even the C++ Core Guidelines strongly recommend against it.
T.47 only advises avoiding highly visible unconstrained templates with common names due to the risks of argument-dependent lookup going wrong. T.10 goes further, recommending that we specify concepts for every template argument to improve both simplicity and readability.
The same idea appears in I.9, which suggests documenting template parameters using concepts.
It’s hard to argue with these guidelines. Concepts make code more readable — just by looking at a function, class, or variable template, the reader can immediately tell what kinds of types are accepted.
If you want to learn more about concepts, check out my concepts-related articles or my book on concepts.
But what makes a good concept? That’s a more complex topic — and one we can’t fully cover in a single article.