Non-type template parameters and requires
If you only have a boolean condition and nothing else, do this:
template<Bla b>
requires(b > 1)
void f() {}
Alternative longer syntax, if you need to check more things in the same requires
-expression:
template<Bla b>
requires requires
{
requires b > 1;
// ^~~~~~~~
}
void f() {}
Since f
needs to be constrained only by the value of the non-type template parameter, you can simply write a requires
clause instead of an ad-hoc requires requires
constraint:
template<Bla b>
requires (b>1)
void f() {}
Here's a demo.
You only need a requires requires
expression if you want to do more complicated checks on the template parameter. In that case, I recommend using a named concept over an ad-hoc constraint anyway. This makes the code more readable, and allows you to reuse the concept in other places.
As for assert
, it's a run-time construct, so it doesn't affect compilation in any way, assuming the expression inside the assert
is syntactically valid. You need to use static_assert
instead, if you want to check the template parameter at compile time:
static_assert(b>1);