GRB*_*GRB 6 c++ compiler-construction gcc templates
请考虑以下代码:
template<int* a>
class base {};
int main()
{
base<(int*)0> test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Comeau和MSVC都没有问题编译(除了关于未使用的变量的Comeau警告),而GCC base<(int*)0> test;在线路上失败,说明
在函数`int main()'中:强制转换为除积分或枚举类型以外的类型不能出现在常量表达式中
模板参数1无效
究竟是什么抱怨的?谁是对的 - 这段代码应该编译吗?值得注意的是,我的GCC版本非常陈旧(3.4.2),因此可能与它有关.谢谢.
从标准草案(重点补充):
14.1.3 A non-type template-parameter shall have one of the following (option-
ally cv-qualified) types:
...
--pointer to object, accepting an address constant expression desig-
nating a named object with external linkage,
...
显然,使用空指针实例化模板是不合法的,因为空指针不指向"具有外部链接的命名对象".