[英]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.