Requires-clause appears after template template parameter: is this legal grammar?

Viewed 204

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?

1 Answers

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 ...opt identifieropt

Now this includes template-head which allows for a requires-clause as mentioned above.

Related