As shown in the cppreference.
The auto
keyword can be used to get rid of template with concept
.
// Constrained C++20 function template:
template<Hashable T>
void f(T) {}
//
// Alternative ways to apply the same constraint:
// template<typename T>
// requires Hashable<T>
// void f(T) {}
//
// template<typename T>
// void f(T) requires Hashable<T> {}
//
void f(Hashable auto /* parameter-name */) {}
My question is how to use the same technic with auto
when I have two template parameters.
template<typename T1, typename T2> concept MyConcept<T1, T2> = ....
template<typename T1, typename T2>
void func(MyConcept<T1, T2> c) { ... }
// Why cannot use :
void func(MyConcept auto c) { ... }
You don't. This syntax only works in the simplest of cases. When a concept is based off of a single type template parameter, that type can be assumed to be the deduced type of the argument (and if it's not, then you'll have to do things differently anyway). But when a concept is based on multiple parameters, which one is the type for the argument, and where does the other type come from?
You have to spell it out long-form with a proper requires
clause.