Search code examples
c++templateslanguage-lawyerlist-initialization

Can a braced initializer be used for non-type template argument in C++?


In the next program the second non-type template argument of struct A is initialized with {} in the alias template B<T>:

template<class T, T>
struct A{};

template<class T>
using B = A<T, {}>;

B<int> b;

GCC is the only compiler accepting this. Both Clang and MSVC reject the program with similar errors. Clang:

error: expected expression

MSVC:

error C2760: syntax error: '{' was unexpected here; expected 'expression'

Demo: https://gcc.godbolt.org/z/6bc3sx451

Which compiler is right here?


Solution

  • I'd say GCC is wrong.

    The grammar for template-argument in [temp.names] says that a template argument must either be a constant-expression, a type-id or an id-expression.

    {} is neither an expression, nor a type, nor an (un)qualified name.