简体   繁体   English

不需要时进行静态初始化

[英]Static initialization when it is not required

Quote from 3.6.2/3 of N3797 C++14 final working draft: 引自N3797 C ++ 14最终工作草案的3.6.2 / 3:

An implementation is permitted to perform the initialization of a non-local variable with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that 允许实现以静态存储持续时间的形式执行非局部变量的初始化作为静态初始化,即使这样的初始化不需要静态地完成,只要这样做,

— the dynamic version of the initialization does not change the value of any other object of namespace scope prior to its initialization, and - 初始化的动态版本在初始化之前不会更改命名空间作用域的任何其他对象的值,并且

— the static version of the initialization produces the same value in the initialized variable as would be produced by the dynamic initialization if all variables not required to be initialized statically were initialized dynamically. - 初始化的静态版本在初始化变量中产生与动态初始化产生的值相同的值,如果所有不需要静态初始化的变量都是动态初始化的。

What does all variable have to initialization of one specific variable? 所有变量对一个特定变量的初始化有什么作用?

If it possible, describe the latter point by example. 如果可能,通过示例描述后一点。

This matters when the initialiser of one variable refers to another variable. 当一个变量的初始化引用另一个变量时,这很重要。

constexpr int f(int);

extern const int a = f(1); // not required to be statically initialized
extern const int b = a; // also not required to be statically initialized

constexpr int f(int x) { return x; }

Now suppose that the implementation chooses to statically initialize b , but dynamically initialize a . 现在假设实现选择静态初始化b ,但动态初始化a In that case, the initialization of b would take place before that of a . 在这种情况下, b的初始化将在b的初始化之前a The text you ask about explains that this doesn't permit an implementation to initialize b to zero: even if b is initialized first, its value must be f(1) , which is 1 . 你问文本有关解释说,这不允许初始化一个实现b为零:即使b首先被初始化,其值必须f(1)这是1

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

相关问题 当静态成员变量的初始化发生时 - When the initialization of static member variable take place 静态初始化 - static initialization 是否需要初始化参考变量主模板,即使它从未实例化过? - Is initialization of a reference variable primary template required even when it is never instantiated? 什么时候使用静态库需要头文件? - When is a header file required for using a static library? 静态 volatile 变量的动态初始化何时发生? - When does dynamic initialization of static volatile variable happen? C++静态初始化命令惨败发生在什么时候? - C++ static initialization order fiasco happens when? dlopen'ing时是否运行静态初始化(和/或其他)代码? - Does static initialization (and/or other) code get run when dlopen'ing? 涉及单例对象时克服静态初始化顺序的失败 - Overcoming static initialization order fiasco when a singleton object is involved 在静态变量初始化中使用cout时的C ++分段错误 - C++ Segmentation Fault when using cout in static variable initialization 什么时候初始化全局常量与外部链接安全从静态初始化顺序fiasco? - When is initialization of global constants with external linkage safe from static initialization order fiasco?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM