静态全局变量初始化顺序

static global variables initialization order

在我在这里找到的许多答案中都说了以下的话:

Global variables in a single translation unit (source file) are initialized in the order in which they are defined.

Within the same compilation unit the order is well defined: The same order as definition.

等等

但是在C++的标准中哪里可以看到这些词呢?我想获得描述此类行为的一个或几个具体段落。自己找不到,也不知道问谁。

6.6.3 Dynamic initialization of non-local variables [basic.start.dynamic]

  1. Dynamic initialization of a non-local variable with static storage duration is unordered if the variable is an implicitly or explicitly instantiated specialization, is partially-ordered if the variable is an inline variable that is not an implicitly or explicitly instantiated specialization, and otherwise is ordered. [ Note: An explicitly specialized non-inline static data member or variable template specialization has ordered initialization. — end note ]
  2. Dynamic initialization of non-local variables V and W with static storage duration are ordered as follows:
    • If V and W have ordered initialization and V is defined before W within a single translation unit, the initialization of V is sequenced before the initialization of W.
    • If V has partially-ordered initialization, W does not have unordered initialization, and V is defined before W in every translation unit in which W is defined, then
      • if the program starts a thread (4.7) other than the main thread (6.6.1), the initialization of V strongly happens before the initialization of W;
      • otherwise, the initialization of V is sequenced before the initialization of W.
    • Otherwise, if the program starts a thread other than the main thread before either V or W is initialized, it is unspecified in which threads the initializations of V and W occur; the initializations are unsequenced if they occur in the same thread.
    • Otherwise, the initializations of V and W are indeterminately sequenced.

引用自 N4659,调整了格式以适应此处支持的降价。

动态初始化的具体定义见前面的6.6.2小节[basic.start.static]。