Difference between revisions of "cpp/language/default initialization"
m (and the final note about the wording change) |
("If T is a const-qualified type, it must be a class type with a user-provided default constructor" statement is also contained in C++03 standard(in 8.5p9), so revision note for it is deleted and statement is removed to Notes section) |
||
Line 37: | Line 37: | ||
{{rev|until=c++11| | {{rev|until=c++11| | ||
If {{tt|T}} is a cv-qualified type, its cv-unqualified version is used for the purpose of default-initialization. | If {{tt|T}} is a cv-qualified type, its cv-unqualified version is used for the purpose of default-initialization. | ||
− | |||
− | |||
− | |||
}} | }} | ||
{{rev end}} | {{rev end}} | ||
Line 69: | Line 66: | ||
===Notes=== | ===Notes=== | ||
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get [[cpp/language/zero_initialization|zero initialized]]) | Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get [[cpp/language/zero_initialization|zero initialized]]) | ||
+ | |||
+ | If {{tt|T}} is a const-qualified type, it must be a class type with a user-provided default constructor. | ||
Reference cannot be default-initialized. | Reference cannot be default-initialized. |
Revision as of 04:45, 21 September 2014
This is the initialization performed when a variable 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 default constructor 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.
|
(until C++11) |
|
(since C++11) |
If |
(until C++11) |
Use of an indeterminate value obtained by default-initializing a non-class variable of any type is undefined behavior, except in the following cases:
int f(bool b) { int x; // value of x is indeterminate int y = x; // undefined behavior unsigned char c; // value of c is indeterminate unsigned char d = c; // OK, value of d is indeterminate int e = d; // undefined behavior return b ? d : 0; // undefined behavior if b is true } |
(since C++14) |
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.
In C++98 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 and specified zero-initialization for non-class types.
Prior to C++11, the definition of default initialization did not include the situations where no initialization is performed; such cases (creating scalars or POD classes without an initializer) were classified as "no initialization".
Example
#include <string> struct T1 {}; class T2 { int mem; public: T2() {} // "mem" not in initializer list }; int n; // A two-phase initialization is done // In the first phase, zero initialization initializes n to zero // In the second phase, default initialization does nothing, leaving n being zero int main() { int n; // non-class: the value is undeterminate std::string s; // calls default ctor, the value is "" (empty string) std::string a[2]; // calls default ctor, creates two empty strings // int& r; // error: default-initializing a reference // const int n; // error: const non-class type // const T1 nd; // error: const class type with implicit ctor T1 t1; // ok, calls implicit default ctor const T2 t2; // ok, calls the user-provided default ctor // t2.mem is default-initialized (to indeterminate value) }