模板非类型参数,C ++ 11,字符串文字的限制 [英] Template Non-Type argument, C++11, restriction for string literals

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

问题描述

模板非类型参数的限制规则如下:

The rules of restrictions for template non-type arguments say:

非类型,非模板模板参数的模板参数应为以下之一:

A template-argument for a non-type, non-template template-parameter shall be one of:

-对于整数或枚举类型的非类型模板参数,该模板参数类型的转换常量表达式(5.19);或

— for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or

-非类型模板参数的名称;或

— the name of a non-type template-parameter; or

-一个常数表达式(5.19),用于指定具有静态存储持续时间和外部或内部链接的对象的地址,或者具有外部或内部链接的函数的地址,包括函数模板和函数模板ID,但不包括非静态类成员,表示为(忽略括号)为 & id-expression,但&如果名称是指函数或数组,则可以省略;如果相应的模板参数是引用,则可以省略;或

— 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, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as & id-expression, except that the & may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or

-一个常量表达式,其结果为空指针值(4.10);或

— a constant expression that evaluates to a null pointer value (4.10); or

-一个常量表达式,其值为空成员指针值(4.11);或

— a constant expression that evaluates to a null member pointer value (4.11); or

-指向成员的指针,如5.3.1中所述.

— a pointer to member expressed as described in 5.3.1.

2 [注意:字符串文字(2.14.5)不满足任何这些类别的要求,因此不是可接受的模板参数.

[ Example:
template<class T, const char* p> class X {
/ ... /
};
X<int, "Studebaker"> x1; // error: string literal as template-argument
const char p[] = "Vivisectionist";
X<int,p> x2; // OK
—end example ] —end note ]

那么为什么字符串文字不能用作非类型参数的参数?

So why string literal cannot used as argument for non-type parameter?

const char arr[5] = "1234";

arr与

"1234"; 

arr具有外部链接,这就是为什么它允许在c ++ 11标准之前将arr用作非类型模板参数.

arr has external linkage and that is why it was allowed to use arr as non-type template argument before c++11 standard.

但是现在指向具有内部链接(静态存储)的对象的指针也可以用作非类型模板参数,而字符串文字也具有内部链接.

But now pointers to objects with internal linkage(static storage) also allowed to use as non-type template arguments and string literal has internal linkage.

推荐答案

允许使用的字符串文字最接近的是您的问题:

The closest that string literals come to being allowed is, from your question:

一个常量表达式(5.19),用于指定具有静态存储持续时间和外部或内部链接的对象的地址

a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage

字符串文字既没有外部链接也没有内部链接,因此不允许使用它们.

String literals have neither external linkage nor internal linkage, so they aren't allowed.

如果您有多个转换单元,每个转换单元都包含具有内部链接的const char arr[5];定义,则这些都是具有不同地址的不同对象,但始终在单个转换单元arr == arr中.实施过程弄清楚了如何使它适用于模板参数.

If you have multiple translation units, each containing a definition of const char arr[5]; with internal linkage, then those are all distinct objects, with distinct addresses, but within a single translation unit, arr == arr, always. Implementations figured out how to make that work for template arguments.

如果您有多个翻译单元,每个翻译单元都包含"1234",则保证它们具有不同的地址.但是即使在单个翻译单元中,也不能保证它们具有相同的地址.

If you have multiple translation units, each containing "1234", then those are not guaranteed to have distinct addresses. Yet even in a single translation unit, they are not guaranteed to have the same address.

如果是"1234" != "1234",则引用模板S<"1234">没有任何意义:每次都将引用不同的模板实例.

If "1234" != "1234", then referring to a template S<"1234"> makes no sense: you'd be referring to a different template instantiation each time.

如果是"1234" == "1234",则要确保每个翻译单元中的S<"1234">是相同类型,实现起来会很复杂.

If "1234" == "1234", then it gets complicated for implementations to make sure that S<"1234"> is the same type in each translation unit.

这篇关于模板非类型参数,C ++ 11,字符串文字的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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