静态常量类成员 [英] Static Constant Class Members

查看:102
本文介绍了静态常量类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

  struct Foo 
{
static const T value = 123; //其中T是一些POD类型
};

const T Foo :: value; //这是必需的吗?

在这种情况下,标准是否需要我们明确声明翻译单元?看来我有冲突的信息; boost和类似numeric_limits的东西从STL似乎做这样的事情就像在我的代码片段。



OTOH,我记得在某个地方(虽然很久以前)读过你仍然需要在翻译单位提供声明。



如果是这样,模板专业化呢?


h2_lin>解决方案

您还必须在翻译单元中提供定义,以便您使用值变量。这意味着,如果你读取它的值。



重要的是,如果违反了该规则,编译器不需要给出警告或错误。该标准表示违反无需诊断。



在下一个C ++标准版本中,规则已更改。当变量用作常量表达式时,不会使用变量。只要读取上面的,即可直接在类中初始化变量,这意味着仍然无需定义。



请参阅部分中 use 的定义3.2一个定义规则的要求,以及 9.4.2,第4段和第5段中的静态数据成员的定义(在C ++ 98标准中)。在下一个标准的n2800草案中有4个)。



更正: c ++ 03已更改的规则:如果变量出现在需要整数常数表达式的地方,则不需要定义(引用2003年更新的非官方修订列表),请参阅此语言缺陷报告


一个表达式可能被评估,除非出现需要一个整数常数表达式见5.19),是sizeof运算符(5.3.3)的操作数,或者是typeid运算符的操作数,并且表达式不指定多态类类型(5.2.8)的左值...


请注意,即使这样,许多用法是在不需要整数常数的情况下使用。其中一个是,是数组维度或模板元编程。因此,严格来说(请参阅此报告),只有c ++ 1x解决方案提供了真正的保证,在明显的情况下也像s == string :: npos其中整数常数不是 required 静态成员的定义是不需要的,因为下一个标准有不同,更好的措辞是3.2。这是相当理论的东西,因为大多数(所有?)编译器不呻吟。感谢评论部分中的人告诉我。


Consider the following snippet:

struct Foo
{
    static const T value = 123; //Where T is some POD-type
};

const T Foo::value; //Is this required?

In this case, does the standard require us to explicitly declare value in a translation unit? It seems I have conflicting information; boost and things like numeric_limits from the STL seem to do this sort of thing just like in my snippet.

OTOH, I remember reading somewhere (albeit a long long time ago) that you're still required to provide a declaration in a translation unit.

If this is the case, what about template specialization? Will each specialization require a declaration?

I'd appreciate your comments as to what the "right way" is.

解决方案

You have to provide a definition in a translation unit too, in case you use the value variable. That means, if for example you read its value.

The important thing is that the compiler is not required to give a warning or error if you violate that rule. The Standard says "no diagnostic required" for a violation.

In the next C++ Standard version, the rule changed. A variable is not used when it is used as a constant expression. Simply reading value above where the variable is initialized directly in the class means that still no definition is required then.

See the definition of use in section 3.2 One Definition Rule of the Standard and requirement for a definition for static data-members in 9.4.2, paragraph 4 and 5 (in the C++98 Standard. Appears in paragraph 3 and 4 in the n2800 draft of the next Standard).

Correction: The rule already changed for c++03: If the variable appears where a integral constant expression is required, no definition is needed (quoting from an unofficial revisions list for the 2003 update), see resolution for this language defect report:

An expression is potentially evaluated unless it appears where an integral constant expression is required (see 5.19), is the operand of the sizeof operator (5.3.3), or is the operand of the typeid operator and the expression does not designate an lvalue of polymorphic class type (5.2.8)...

Note that even then, many uses are in cases where an integral constant is not required. Cases where one is, is in array dimensions or in template metaprogramming. So strictly speaking (see this report), only the c++1x solution provides really guarantee that in obvious cases also like "s == string::npos" where an integral constant is not required the definition of the static member is not needed, because the next Standard has a different, better wording of 3.2. This is however quite theoretical stuff, since most (all?) compiler don't moan anyway. Thanks for the guy in the comment section for telling me.

这篇关于静态常量类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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