[basic.start.static] (original) (raw)

6 Basics [basic]

6.9 Program execution [basic.exec]

6.9.3 Start and termination [basic.start]

6.9.3.2 Static initialization [basic.start.static]

Variables with static storage duration are initialized as a consequence of program initiation.

Variables with thread storage duration are initialized as a consequence of thread execution.

Within each of these phases of initiation, initialization occurs as follows.

Constant initialization is performed if a variable with static or thread storage duration is constant-initialized ([expr.const]).

If constant initialization is not performed, a variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is zero-initialized ([dcl.init]).

Together, zero-initialization and constant initialization are calledstatic initialization; all other initialization is dynamic initialization.

All static initialization strongly happens before ([intro.races]) any dynamic initialization.

[Note 1:

The dynamic initialization of non-block variables is described in [basic.start.dynamic]; that of static block variables is described in [stmt.dcl].

— _end note_]

An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such initialization is not required to be done statically, provided that

[Note 2:

As a consequence, if the initialization of an object obj1 refers to an object obj2 potentially requiring dynamic initialization and defined later in the same translation unit, it is unspecified whether the value of obj2 used will be the value of the fully initialized obj2 (because obj2 was statically initialized) or will be the value of obj2 merely zero-initialized.

For example,inline double fd() { return 1.0; } extern double d1;double d2 = d1; double d1 = fd();

— _end note_]