模板非类型参数 [英] template non type arguments

查看:125
本文介绍了模板非类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

    

$ 14.3.2 - ......一个模板参数的非类型,非模板的模板参数应为之一:


  
  
  

...恒定前pression(5.19),指定具有静态存储时间和外部或内部联动或者与外部或内部连接函数的对象的地址...


在下面显示的code,我不明白为什么名2'和'NAME3'不允许作为非类型模板参数。我在Windows上使用GCC 4.7.2。

这两个'名2'和'NAME3是数组的名字,因此是不变的前pressions。另外名2'是有内在联系和'NAME3'有静态和内部链接。

 模板<字符常量* P>无效F()
{}烧焦名1 [] =嗨;
静态CHAR名2 [] =嗨;
常量静态字符NAME3 [] =嗨;
字符* NAME4 =嗨;诠释的main()
{
    F<&NAME1 GT;();
    F<&NAME2 GT;();
    F<&NAME3 GT;();
    F<&NAME4 GT;();
}


解决方案

由于@Nawaz猜中,这是一个实现的bug,而不是标准的一个神秘角落。

具体而言,海湾合作委员会似乎有麻烦了。除非最后 NAME4 这是对标准,它的其余部分编译罚款的

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆