Concepts can be used to put a constraint on types as template parameters like the example below:
template<typename t, int v>
concept the_concept1 = sizeof(t) > v;
template<int v, the_concept1<v> t>
struct some_struct1{};
I am trying to use a similar method with values like the example below:
template<int v1, int v2>
concept the_concept2 = v1 > v2;
template<int v1, the_concept2<v1> v2>
struct some_struct2{};
But with G++ 10 I am getting the following error message:
error: ‘the_concept2’ does not constrain a type
So I was wondering if concepts can be used to put a constraint on values? If so then how should I do it?
Edit: My final goal is to use the concept in declaration of a template structure with variadic template parameters like:
template<typename t, std::size_t ... v>
struct the_struct;
And I need a concept to check if every v
is less than sizeof(t)
.
If you want to use a concept as a named type constraint on a template parameter, as in your example, the concept needs to apply to a type template parameter.
You can still define concepts that apply only to e.g. non-type template parameters, however, as long as you use it in a context which allows these; e.g. using a requires-clause:
Another example applied on a function template or a member function of a class template:
The following OP edit (which basically asks an entirely different question)
can be achieved by specifying the concept itself to apply for variadic non-type template parameters that are expanded in the
sizeof(T) > v
check using parameter pack expansion: