In my opinion, the static variable inside the function should be initialized even before main ()
Your mind is wrong ... at least in part. A static local variable can be initialized early in some situations, but not when the constructor depends on a local variable such as this.
n3242 draft standard Β§6.7 / 4:
... Implementation is allowed for early initialization of other variables of a block area with static or stream storages under the same conditions that implementations are allowed to statically initialize a variable with statics or duration of storage of streams in the namespace (3.6.2). Otherwise, such a variable is initialized when the first control passes through its declaration; ...
For completeness, here are the requirements for constant (static) initialization Β§ 3.3.2 / 2:
Continuous initialization is performed:
- if every complete expression (including implicit conversions) that appears in the link initializer with a static or storage duration of streams is a constant expression (5.19), and the link is bound to the value lvalue, the destination of the object with a static storage time or temporary (see 12.2);
- if the object with the statics or the duration of the storage of the stream is initialized by calling the constructor, if the constructor is constructor constexpr, if all the constructor arguments are constant expressions (including conversions), and if, after substituting the function call (7.1.5), each constructor call and the full expression in mem- initializers is a constant expression;
- if the object with the statics or duration of the storage of the stream is not initialized by calling the constructor, and if each complete expression that appears in its initializer is a constant expression.
1) x initialized when execution reaches its declaration for the first time and that when the constructor starts. So b fully initialized when x initializes.
2) Regarding initialization dependencies, yes.
3) Of course, if you need it, the constructor of a static local object may depend on the local object. Until you refer to this local object after its absence. In this case, you simply copy it, so you will not depend on it after building x .