简体   繁体   English

命名空间是否会影响C ++中的初始化顺序?

[英]Do namespaces affect initialization order in C++?

Global variables are initialized in order of appearing in the translation module and the relative order of initialization of variables in different translation modules in unspecified (so-called "static initialization order fiasco"). 全局变量按照出现在翻译模块中的顺序以及未指定的不同翻译模块中的变量初始化的相对顺序(所谓的“静态初始化顺序惨败”)进行初始化。

Do namespaces have any influence on that? 名称空间对此有影响吗? For example if I have this code: 例如,如果我有这个代码:

//first.cpp
int first;
int second;

will it have any difference in initialization order compared to this code: 与此代码相比,初始化顺序是否有任何差异:

//second.cpp
namespace {
int first;
}
int second;

Are there cases where putting a global object into a namespace affects initialization order? 是否存在将全局对象放入命名空间会影响初始化顺序的情况?

3.6 Other non-local variables with static storage duration have ordered initialization. 3.6具有静态存储持续时间的其他非局部变量已经有序初始化。 Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit. 在单个翻译单元中定义的有序初始化的变量应按其在翻译单元中的定义顺序进行初始化。

Namespaces have no effect on this - not mentioned in the section. 命名空间对此没有影响 - 本节中未提及。

What does effect the order is different translation units. 什么影响订单是不同的翻译单位。 If you need to define the order across them, use an extension such as GCC's constructor attribute. 如果需要在它们之间定义顺序,请使用扩展名,例如GCC的constructor属性。

Well, the "Global variables are initialized in order of appearing in the translation module" is definite. 那么,“全局变量按照出现在翻译模块中的顺序进行初始化”是明确的。 It does not leave any room for anything else, like namespaces, to affect the order. 它不会留下任何其他任何空间,如命名空间,以影响订单。

Actually, "Global variables are initialized in order ..." is imprecise quotation of the standard as is formally wrong. 实际上,“全局变量按顺序初始化......”是标准的不精确引用,因为这是正式错误的。 The exact wording from C++ Standard, ISO/IEC 14882:2003, 3.6.2 paragraph 1 is: C ++标准,ISO / IEC 14882:2003,3.6.2第1段的确切措辞是:

Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit. 在同一翻译单元的命名空间范围内定义并动态初始化的静态存储持续时间的对象应按其定义出现在翻译单元中的顺序进行初始化。

So rather than "global" it says "with static storage", that is all non-local variables whether they are global, namespace members or class members and whether they are declared static or not. 因此,不是“全局”它表示“使用静态存储”,这是所有非本地变量,无论它们是全局变量,命名空间成员还是类成员,以及它们是否被声明为static

Also it adds "and dynamically initialized". 它还增加了“动态初始化”。 Variables with trivial constructors and constant initializer are always initialized first (by simply loading their values from the binary) and than all non-constant initializers are evaluated and non-trivial constructors are run in that order. 具有普通构造函数和常量初始值设定项的变量总是首先初始化(通过简单地从二进制加载它们的值),并且评估所有非常量初始化程序,并且按顺序运行非平凡构造函数。 This is important, so you can for example reliably create a linked list in those constructors; 这很重要,因此您可以在这些构造函数中可靠地创建链接列表; if it's head is plain pointer, it is already initialized, so you can safely work with it. 如果它的头是普通指针,它已经初始化,所以你可以安全地使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM