是否可以保证静态类成员在调用`main`之前被初始化? [英] Are static class members guaranteed to be initialized before `main` is called?

查看:70
本文介绍了是否可以保证静态类成员在调用`main`之前被初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以保证在调用 main 之前初始化静态类成员?

Is there any guarantee that static class members are initialized before main is called?

推荐答案

我认为:

[C ++ 03:3.6.2/3]: 是否由动态初始化来实现定义(8.5、9.4、12.1、12.6.1)名称空间范围对象的 main 的第一条语句之前完成.如果初始化推迟到 main的第一条语句之后的某个时间点,它应在首次使用与要初始化的对象相同的转换单元中定义的任何函数或对象之前发生.

[C++03: 3.6.2/3]: It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.


嗯,真的吗?

可以说,在命名空间范围内定义"与命名空间范围的对象"不同:


Hmm, really?

Well, arguably, "defined in namespace scope" is not quite the same thing as "an object of namespace scope":

[C ++ 03:9.4.2/2]:在其类定义中的 static 数据成员的声明不是定义,并且可以是除cv合格的 void 以外的不完整类型.静态代码成员的定义应出现在包含成员类定义的名称空间范围中.在命名空间范围的定义中, static 数据成员的名称应使用 :: 运算符通过其类名来限定.初始化器静态数据成员的定义中的表达式属于其类(3.3.6)的范围.

[C++03: 9.4.2/2]: The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member's class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class (3.3.6).

但是,在类范围内的是 initializer ;没有提及 static 成员本身具有除名称空间范围以外的任何内容(除非我们在每个地方都在脑海中注入"lexically" 一词).

However, it's the initializer that's in the class's scope; there's no mention of the static member itself having anything other than namespace scope (unless we mentally inject the word "lexically" everywhere).

这是一段令人愉快的段落:

There is this pleasing paragraph:

[C ++ 03:9.4.2/7]:静态数据成员的初始化和销毁​​完全类似于非本地对象(3.6.2、3.6.3).

[C++03: 9.4.2/7]: Static data members are initialized and destroyed exactly like non-local objects (3.6.2, 3.6.3).

但是,不幸的是,相对于非本地对象", main 的排序和静态初始化的唯一进一步定义是上述的 [C ++ 03]:3.6.2/3] .

However, unfortunately, the only further definition of the sequencing of main and static initialisation, with respect to "non-local objects", is the aforementioned [C++03: 3.6.2/3].

我相信,C ++ 11中新的措辞可以解决所有问题:

I believe that the intent of this otherwise potentially ambiguous rule is clearly shown by the new wording in C++11, which resolves everything:

[C ++ 11:9.4.2/6]:静态数据成员的初始化和销毁​​与非本地变量(3.6.2,3.6.3)完全一样.

[C++11: 9.4.2/6]: Static data members are initialized and destroyed exactly like non-local variables (3.6.2, 3.6.3).

[C ++ 11:3.6.2/4]:由main的第一条语句之前是否完成具有静态存储持续时间的非局部变量的动态初始化由实现定义.. [..]

[C++11: 3.6.2/4]: It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. [..]

这篇关于是否可以保证静态类成员在调用`main`之前被初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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