静态全局变量初始化顺序 [英] static global variables initialization order

查看:127
本文介绍了静态全局变量初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我在这里找到的许多答案中,都说了以下几个字:

In many of the answers that I found here were said the following words:

单个翻译单元(源文件)中的全局变量按照定义顺序进行初始化.

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 ++标准中我在哪里可以看到这些词?我想获得描述这种行为的一个或几个具体段落.我自己找不到,也不知道问谁.

But where can I see these words in the standard of C++? I would like to get a one or few concrete paragraph's where such behavior is described. I can not find it myself, and I do not know who to ask.

推荐答案

6.6.3非局部变量的动态初始化[ basic.start.dynamic]

  1. 非本地的动态初始化如果变量为静态,则具有静态存储持续时间的变量是无序的隐式或显式实例化的专业化是如果变量是不是一个内联变量的部分排序隐式或显式实例化的专业化,否则为下令.[注意:显式专门化的非内联静态数据成员或变量模板专门化已命令初始化.—尾注]
  2. 非局部变量V和W的动态初始化具有静态存储持续时间的顺序如下:
    • 如果V和W已排序初始化,并且V在A中的W之前定义单个翻译单元,V的初始化先于W的初始化.
    • 如果V具有部分有序的初始化,W没有无序初始化,并且在每个翻译单元中的W之前定义V定义哪个W,然后
      • 如果程序启动了线程(4.7)除主线程(6.6.1)之外,初始化VW强烈地发生在W的初始化之前;
      • 否则,V的初始化在W的初始化之前进行排序.

从N4659引用,调整格式以与此处支持的降价一起使用.

Quoted from N4659, formatting adjusted to work with the markdown supported here.

有关动态初始化的确切定义,请参见前面的第6.6.2节[basic.start.static].

For the exact definition of dynamic initialization, see the preceding subsesction 6.6.2 [basic.start.static].

这篇关于静态全局变量初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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