Difference between revisions of "cpp/language/default initialization"
m ("when {a variable -> an object} is constructed") |
(→Explanation: CWG 253 (RU1, P0490R0), applied to C++98.) |
||
Line 37: | Line 37: | ||
}} | }} | ||
{{rev end}} | {{rev end}} | ||
+ | |||
+ | ====Default initialization of a const object==== | ||
+ | If a program calls for the default-initialization of an object of a {{rlp|cv|const}}-qualified type {{tt|T}}, T shall be a ''const-default-constructible'' class type or array thereof. | ||
+ | |||
+ | A class type {{tt|T}} is const-default-constructible if default initialization of {{tt|T}} would invoke a user-provided constructor of {{tt|T}} {{rev inl|since=c++11|(not inherited from a base class)}} or if | ||
+ | {{rrev multi|until1=c++11|rev1= | ||
+ | * each direct non-static data member {{tt|M}} of {{tt|T}} is of class type {{tt|X}} (or array thereof), {{tt|X}} is const-default-constructible, and | ||
+ | * {{tt|T}} has no direct {{rlp|union#Union-like classes|variant members}}, and | ||
+ | |rev2= | ||
+ | * each direct non-variant non-static data member {{tt|M}} of {{tt|T}} has a {{rlp|data members#Member initialization|default member initializer}} or, if {{tt|M}} is of class type {{tt|X}} (or array thereof), {{tt|X}} is const-default-constructible, | ||
+ | * if {{tt|T}} is a union with at least one non-static data member, exactly one {{rlp|union#Union-like classes|variant member}} has a default member initializer, | ||
+ | * if {{tt|T}} is not a union, for each anonymous union member with at least one non-static data member (if any), exactly one non-static data member has a default member initializer, and | ||
+ | }} | ||
+ | each potentially constructed base class of {{tt|T}} is const-default-constructible. | ||
====Read from an indeterminate byte==== | ====Read from an indeterminate byte==== |
Revision as of 07:37, 23 November 2020
This is the initialization performed when an object is constructed with no initializer.
Contents |
Syntax
T object ;
|
(1) | ||||||||
new T
|
(2) | ||||||||
Explanation
Default initialization is performed in three situations:
The effects of default initialization are:
- if
T
is a non-POD(until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object; - if
T
is an array type, every element of the array is default-initialized; - otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Only (possibly cv-qualified) non-POD class types (or arrays thereof) with automatic storage duration were considered to be default-initialized when no initializer is used. Scalars and POD types with dynamic storage duration were considered to be not initialized (since C++11, this situation was reclassified as a form of default initialization). |
(until C++11) |
Prior to C++03 (which introduced value initialization), the expression new T() as well as a member initializer naming a base or a member with the initializer in the form of an empty pair of parentheses was classified as default initialization, but specified zero initialization for non-class types. |
(until C++03) |
If |
(until C++11) |
Default initialization of a const object
If a program calls for the default-initialization of an object of a const-qualified type T
, T shall be a const-default-constructible class type or array thereof.
A class type T
is const-default-constructible if default initialization of T
would invoke a user-provided constructor of T
(not inherited from a base class)(since C++11) or if
|
(until C++11) |
|
(since C++11) |
each potentially constructed base class of T
is const-default-constructible.
Read from an indeterminate byte
Use of an indeterminate value obtained by default-initializing a non-class variable of any type is undefined behavior (in particular, it may be a trap representation), except in the following cases:
- if an indeterminate value of type unsigned char or std::byte(since C++17) is assigned to another variable of type (possibly cv-qualified) unsigned char or std::byte(since C++17)(the value of the variable becomes indeterminate, but the behavior is not undefined);
- if an indeterminate value of type unsigned char or std::byte(since C++17) is used to initialize another variable of type (possibly cv-qualified) unsigned char or std::byte(since C++17);
- if an indeterminate value of type unsigned char or std::byte(since C++17) results from
- the second or third operand of a conditional expression,
- the right operand of the comma operator,
- the operand of a cast or conversion to (possibly cv-qualified) unsigned char or std::byte(since C++17),
- a discarded-value expression.
int f(bool b) { int x; // OK: the value of x is indeterminate int y = x; // undefined behavior unsigned char c; // OK: the value of c is indeterminate unsigned char d = c; // OK: the value of d is indeterminate int e = d; // undefined behavior return b ? d : 0; // undefined behavior if b is true }
Notes
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
If T
is a const-qualified type, it must be a class type with a user-provided default constructor.
Reference cannot be default-initialized.
Example
#include <string> struct T1 { int mem; }; struct T2 { int mem; T2() { } // "mem" is not in the initializer list }; int n; // static non-class, a two-phase initialization is done: // 1) zero initialization initializes n to zero // 2) default initialization does nothing, leaving n being zero int main() { int n; // non-class, the value is indeterminate std::string s; // class, calls default ctor, the value is "" (empty string) std::string a[2]; // array, default-initializes the elements, the value is {"", ""} // int& r; // error: a reference // const int n; // error: a const non-class // const T1 t1; // error: const class with implicit default ctor T1 t1; // class, calls implicit default ctor const T2 t2; // const class, calls the user-provided default ctor // t2.mem is default-initialized (to indeterminate value) }
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 616 | C++98 | lvalue to rvalue conversion of any uninitialized object was always UB | indeterminate unsigned char is allowed |
CWG 1787 | C++98 | read from an indeterminate unsigned char cached in a register was UB | made well-defined |