< cpp‎ | language
Revision as of 15:12, 12 June 2013 by (Talk)

C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Alternative representations
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Attributes (C++11)
typedef declaration
Type alias declaration (C++11)
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Class-specific function properties
Special member functions

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 declarator. 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:

( expression-list ) (1)
= expression (2)
{ initializer-list } (3)
1) comma-separated list of arbitrary expressions in parentheses
2) the equals sign followed by an expression
3) braced-init-list: possibly empty, comma-separated list of expressions and other braced-init-lists

Depending on context, initializer may invoke one of the following:

If no initializer is provided, the rules of default initialization apply.

Non-local variables

All variables with static storage duration are initialized as part of program startup, before the execution of the main function begins. All variables with thread-local storage duration are initialized as part of thread launch, before the execution of the thread function begins. For both of these classes of variables, initialization occurs in stages:

1) Static initialization of non-local variables happens first, and consists of two steps
a) Zero initialization takes place before anything else (in practice, variables that are going to be zero-initialized, are typically placed in the .bss segment, which is zeroed out by the OS when loading the program)
b) Constant initialization takes place where applicable. (in practice, this stage is usually performed at compile time, and pre-calculated values are stored as part of the program image. If the compiler doesn't do that, it still has to guarantee that this takes place before any dynamic initialization)
2) Dynamic initialization of non-local variables, which takes place after all static initialization is completed, can be one of:
b) Unordered dynamic initialization, which applies only to (static/thread-local) class template data members that aren't explicitly specialized. Initialization of such static variables is undeterminately sequenced with respect to all other dynamic initialization. Initialization of such thread-local variables is unsequenced with respect to all other dynamic initialization.
a) Ordered dynamic initialization, which applies to all other non-local variables: within a single translation unit, these variables are initialized 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.

The compilers are allowed to initialize dynamically-initialized variables as part of static initialization (essentially, at compile time), if both of these are 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.