Recently, I unexpectedly found that gcc and msvc accept the following code (notice the requires-clause in template-list):
#include <vector>
template <template <class> requires true class>
void f() {}
int main() {
f<std::vector>();
}
and clang rejects its grammar with:
<source>:3:28: error: template template parameter requires 'class' after the parameter list
template <template <class> requires true class>
^
Which compiler should I trust? Is this code grammatically valid?
A requires clause uses a compile-time Boolean expression to define requirements on template arguments or function declarations. It affects the behavior of a program, determining whether a function participates in overload resolution or not or whether a template instantiation is valid.
Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
template<typename T> requires { requires Eq<T>; } void f(T a, T b); Here, the 2nd requires is called a nested-requirement; it evaluates its expression (other code in the block of the requires-expression is not evaluated). I think this is way worse than the status quo. Now, you get to write requires twice everywhere.
Yes, this appears to be legal grammar. A template begins with a template-head which is
template
<
template-parameter-list>
requires-clause opt
then a template-parameter-list can be
template-parameter
where template-parameter can be
type-parameter
and then type-parameter can be
template-head type-parameter-key
...
optidentifieropt
Now this includes template-head which allows for a requires-clause as mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With