cpp/language/initialization

Initialization of a variable provides its initial value at the time of construction.

The initial value may be provided in the initializer section of a or a. It also takes place during function calls: function parameters and the function return values are also initialized.

For each declarator, the initializer may be one of the following:

@1@ comma-separated list of arbitrary expressions and braced-init-lists in parentheses @2@ the equals sign followed by an expression or a braced-init-list @3@ braced-init-list: possibly empty, comma-separated list of expressions and other braced-init-lists @4@ a braced-init-list with

Depending on context, the initializer may invoke:
 * , e.g.
 * , e.g.
 * , e.g.
 * , e.g.
 * , e.g.
 * , e.g.

If no initializer is provided, the rules of apply.

Initialization includes the evaluation of all subexpressions within the initializer and the creation of any for function arguments or return values.

Non-local variables
All non-local variables with static are initialized as part of program startup, before the execution of the  begins (unless deferred, see below). All non-local variables with thread-local storage duration are initialized as part of thread launch, sequenced-before the execution of the thread function begins. For both of these classes of variables, initialization occurs in two distinct stages:

Static initialization
There are two forms of static initialization: @1@ If possible, is applied.

@2@ Otherwise, non-local static and thread-local variables are.

In practice:
 * Constant initialization is usually applied at compile time. Pre-calculated object representations are stored as part of the program image. If the compiler doesn't do that, it must still guarantee that the initialization happens before any dynamic initialization.
 * Variables to be zero-initialized are placed in the segment of the program image, which occupies no space on disk and is zeroed out by the OS when loading the program.

Dynamic initialization
After all static initialization is completed, dynamic initialization of non-local variables occurs in the following situations:

@1@ Unordered dynamic initialization, which applies only to (static/thread-local) class template  that aren't. Initialization of such static variables is indeterminately sequenced with respect to all other dynamic initialization. Initialization of such thread-local variables is unsequenced with respect to all other dynamic initialization.

@3@ Ordered dynamic initialization, which applies to all other non-local variables: within a single translation unit, initialization of these variables is always in exact order their definitions appear in the source code. Initialization of static variables in different translation units is indeterminately sequenced. Initialization of thread-local variables in different translation units is unsequenced.

If the initialization of a non-local variable with static or thread storage duration exits via an exception, std is called.

Early dynamic initialization
The compilers are allowed to initialize dynamically-initialized variables as part of static initialization (essentially, at compile time), if the following conditions are both true: @1@ the dynamic version of the initialization does not change the value of any other object of namespace scope prior to its initialization @2@ 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.

Because of the rule above, if initialization of some object refers to a namespace-scope object, which potentially requires dynamic initialization, but is defined later in the same translation unit, it is unspecified whether the value of  used will be the value of the fully initialized  (because the compiler promoted initialization of  to compile time) or will be the value of  merely zero-initialized.

Deferred dynamic initialization
It is implementation-defined whether dynamic initialization happens-before the first statement of the main function (for statics) or the initial function of the thread (for thread-locals), or deferred to happen after.

If the initialization is deferred to happen after the first statement of main/thread function, it happens before the first  of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized. If no variable or function is odr-used from a given translation unit, the non-local variables defined in that translation unit may never be initialized (this models the behavior of an on-demand dynamic library). However, as long as anything from a translation unit is odr-used, all non-local variables whose initialization or destruction has side effects will be initialized even if they are not used in the program.

Static local variables
For initialization of locals (that is, block scope) static and thread-local variables, see.

Initializer is not allowed in a block-scope declaration of a variable with. Such a declaration must appear with and cannot be a definition.

Class members
Non-static data members can be initialized with or with a.