1) If I'm not mistaken, C++ standard guarantees that static variables in a single translation unit are initialized in their definition order. And I'm confused about the following code fragment:
extern int n;
int k = n;
int n = 2;
extern int n;
is the declaration, not the definition, so k
is defined before n
, but GCC, Clang and MSVC all show me that k == 2
after the initialization of the global variables. For me it's unclear how can k
be assigned 2 after int k = n;
, because n
is not initialized at that point yet and its value must be zero.
If we change the last line to:
int n = func();
where func()
is non-constexpr, then k
will be assigned zero, as I expect. So, do initialization of the global variables at the compile time change the order of the initialization?
2) Here is another code fragment:
class Base
{
public:
struct static_constructor
{
static_constructor()
{
i = 1;
}
};
static static_constructor constructor;
static int i;
};
Base::static_constructor Base::constructor;
int Base::i = 2;
When Base::constructor
is defined, its constructor is called, and i = 1
assignment is performed. But at this point Base::i
is not defined yet, so, please, could you explain me what happens at this point and why is Base::i
equal to 1 in the end?
The first scenario is well-defined in [basic.start.init]/2:
Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.
Constant initialization is performed:
- if each full-expression (including implicit conversions) that appears in the initializer of a reference with static or thread storage duration is a constant expression (5.19) and the reference is bound to an lvalue designating an object with static storage duration or to a temporary (see 12.2);
- if an object with static or thread storage duration is initialized by a constructor call, if the constructor is a
constexpr
constructor, if all constructor arguments are constant expressions (including conversions), and if, after function invocation substitution (7.1.5), every constructor call and full-expression in the mem-initializers and in the brace-or-equal-initializers for non-static data members is a constant expression;- if an object with static or thread storage duration is not initialized by a constructor call and if every full-expression that appears in its initializer is a constant expression.
Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place. (...)
(Emphasis mine)
The upshot of this fairly lengthy paragraph is that
int n = 2;
is static initialization, while
int k = n;
is dynamic initialization (because n
is not a constant expression), and therefore n
is initialized before k
even if it appears later in the code.
The same logic applies in the case of the Base::static_constructor
example -- because the constructor of Base::static_constructor
is not constexpr
, Base::constructor
is dynamically initialized, whereas Base::i
is statically initialized. The initialization of Base::i
therefore takes place before the initialization of Base::constructor
.
On the other hand, the second case with
int n = func();
puts you squarely in the territory of unspecified behavior, and it is quite explicitly mentioned in [basic.start.init]/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.
[Note: As a consequence, if the initialization of an object
obj1
refers to an objectobj2
of namespace scope potentially requiring dynamic initialization and defined later in the same translation unit, it is unspecified whether the value ofobj2
used will be the value of the fully initializedobj2
(becauseobj2
was statically initialized) or will be the value ofobj2
merely zero-initialized. For example,inline double fd() { return 1.0; } extern double d1; double d2 = d1; // unspecified: // may be statically initialized to 0.0 or // dynamically initialized to 0.0 if d1 is // dynamically initialized, or 1.0 otherwise double d1 = fd(); // may be initialized statically or dynamically to 1.0
-- end note]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With