What are the different ways to apply a constraint to a template?
Short answer
You can constrain templates via: a concept name in place of typename (template ), a requires clause (template requires C ), a trailing requires clause after the function signature, or the abbreviated function template syntax using a concept with auto (void f(std::integral auto…
Advertisement
AModel answer
You can constrain templates via: a concept name in place of typename (template <std::integral T>), a requires clause (template <class T> requires C<T>), a trailing requires clause after the function signature, or the abbreviated function template syntax using a concept with auto (void f(std::integral auto x)). All express the same kind of requirement.
void f(std::integral auto x); // abbreviated
template <class T> requires C<T> void g(T); // requires clauseReal-world example
The team uses the abbreviated 'std::sortable auto' parameter form for concise constrained functions.
Likely follow-ups
- What is the abbreviated function template syntax? | What is a requires clause?
Related topics