花括号初始化程序可以用于 C++ 中的非类型模板参数吗?
Can a braced initializer be used for non-type template argument in C++?
在下一个程序中,struct A
的第二个非类型模板参数在别名模板 B<T>
:
中用 {}
初始化
template<class T, T>
struct A{};
template<class T>
using B = A<T, {}>;
B<int> b;
GCC 是唯一接受这个的编译器。 Clang 和 MSVC 都拒绝了具有类似错误的程序。叮当声:
error: expected expression
MSVC:
error C2760: syntax error: '{' was unexpected here; expected 'expression'
演示:https://gcc.godbolt.org/z/6bc3sx451
这里是哪个编译器?
我会说 GCC 是错误的。
[temp.names] 中 template-argument 的语法说模板参数必须是 constant-expression, type-id 或 id-expression.
{}
既不是表达式,也不是类型,也不是(非)限定名称。
在下一个程序中,struct A
的第二个非类型模板参数在别名模板 B<T>
:
{}
初始化
template<class T, T>
struct A{};
template<class T>
using B = A<T, {}>;
B<int> b;
GCC 是唯一接受这个的编译器。 Clang 和 MSVC 都拒绝了具有类似错误的程序。叮当声:
error: expected expression
MSVC:
error C2760: syntax error: '{' was unexpected here; expected 'expression'
演示:https://gcc.godbolt.org/z/6bc3sx451
这里是哪个编译器?
我会说 GCC 是错误的。
[temp.names] 中 template-argument 的语法说模板参数必须是 constant-expression, type-id 或 id-expression.
{}
既不是表达式,也不是类型,也不是(非)限定名称。