为什么这个有效呢?
但这个(使用常量变量)不行吗?
char __nontype[] = "foo";
typedef TemplateClass<T, __nontype> MyClass;
但这个(使用常量变量)不行吗?
const char __nontype[] = "foo";
typedef TemplateClass<T, __nontype> MyClass;
编译器错误:
错误:'__nontype'不能出现在常量表达式中
错误:模板参数2无效
const
修饰符会自动使某些东西成为constexpr
。字符数组不是编译时常量;它们直到链接时才被知道。 - tenfourchar
数组是可以的。这是因为一些链接时常量表达式是有效的模板参数。 - user743382