模板非类型参数 [英] template non type arguments
问题描述
$14.3.2 - "... 非类型、非模板模板参数的模板参数应为:
...一个常量表达式 (5.19),用于指定具有静态存储持续时间和外部或内部链接的对象的地址或具有外部或内部链接的函数..."
在下面显示的代码中,我无法理解为什么不允许将name2"和name3"作为非类型模板参数.我在 Windows 上使用 gcc 4.7.2.
'name2' 和 'name3' 都是数组的名称,因此都是常量表达式.此外,name2"具有内部链接,而name3"具有静态链接和内部链接.
template无效 f(){}char name1[] = "你好";静态字符名称2[]=嗨";const static char name3[]= "嗨";char *name4 = "你好";int main(){f();f 名称2 ();f ();f();}
正如@Nawaz 猜到的那样,这是一个实现错误,而不是标准的一个深奥的角落.
具体来说,gcc 似乎有问题.除了最后一个违反标准的 name4
之外,它的其余部分与 clang 编译得很好
$14.3.2 - "... A template-argument for a non-type, non-template template-parameter shall be one of:
...a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage..."
In the code shown below, I fail to understand why 'name2' and 'name3' are not allowed as non type template arguments. I am using gcc 4.7.2 on Windows.
Both 'name2' and 'name3' are names of array and hence are constant expressions. Further 'name2' is having internal linkage and 'name3' has both static and internal linkage.
template<char const *p> void f()
{
}
char name1[] = "Hi";
static char name2[]= "Hi";
const static char name3[]= "Hi";
char *name4 = "Hi";
int main()
{
f<name1>();
f<name2>();
f<name3>();
f<name4>();
}
As @Nawaz correctly guessed, this is an implementation bug, not an esoteric corner of the standard.
Specifically, gcc seems to have trouble with it. Barring the last name4
which is against the standard, the rest of it compiles fine with clang
这篇关于模板非类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!