This is the initialization performed when an object is constructed with no initializer.
[edit] Syntax T object ;
(1) new
T (2) [edit] Explanation
Default-initialization is performed in three situations:
1)when a variable with automatic, static, or thread-local
storage durationis declared with no initializer;
2)when an object with dynamic storage duration is created by a
new-expressionwith no initializer;
The effects of default-initialization are:
T
is a (possibly cv-qualified) 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;T
is an array type, every element of the array is default-initialized;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
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)M
of T
is of class type X
(or array thereof), X
is const-default-constructible, andT
has no direct variant members, andM
of T
has a default member initializer or, if M
is of class type X
(or array thereof), X
is const-default-constructible,T
is a union with at least one non-static data member, exactly one variant member has a default member initializer,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, andeach potentially constructed base class of T
is const-default-constructible.
When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value.
If no initialization is performed for an object, that object retains an indeterminate value until that value is replaced.
(until C++26)When storage for an object with automatic or dynamic storage duration is obtained, the bytes comprising the storage for the object have the following initial value:
[[indeterminate]]
, the bytes have indeterminate values.If no initialization is performed for an object (including subobjects), such a byte retains its initial value until that value is replaced.
If an evaluation produces an indeterminate value, the behavior is undefined.
If an evaluation produces an erroneous value, the behavior is erroneous.
(since C++26) [edit] Special casesThe following types are uninitialized-friendly:
Given an indeterminate or erroneous(since C++26) value value, the uninitialized result value of value is:
If an evaluation eval produces an indeterminate or erroneous(since C++26) value value of an uninitialized-friendly type, the behavior is well-defined in the following cases:
static_cast
to an uninitialized-friendly type.Converting an indeterminate value of an uninitialized-friendly type produces an indeterminate value.
Converting an erroneous value of an uninitialized-friendly type produces an erroneous value, the result of the conversion is the value of the converted operand.
(since C++26)// Case 1: Uninitialized objects with dynamic storage duration // All C++ versions: indeterminate value + undefined behavior int f(bool b) { unsigned char* c = new unsigned char; unsigned char d = *c; // OK, âdâ has an indeterminate value int e = d; // undefined behavior return b ? d : 0; // undefined behavior if âbâ is true } // Case 2: Uninitialized objects with automatic storage duration // until C++26: indeterminate value + undefined behavior // since C++26: erroneous value + erroneous behavior int g(bool b) { unsigned char c; // âcâ has an indeterminate/erroneous value unsigned char d = c; // no undefined/erroneous behavior, // but âdâ has an indeterminate/erroneous value assert(c == d); // holds, but both integral promotions have // undefined/erroneous behavior int e = d; // undefined/erroneous behavior return b ? d : 0; // undefined/erroneous behavior if âbâ is true } // Same as case 2 void h() { int d1, d2; // âd1â and âd2â have indeterminate/erroneous values int e1 = d1; // undefined/erroneous behavior int e2 = d1; // undefined/erroneous behavior assert(e1 == e2); // holds assert(e1 == d1); // holds, undefined/erroneous behavior assert(e2 == d1); // holds, undefined/erroneous behavior // no undefined/erroneous behavior, // but âd2â has an indeterminate/erroneous value std::memcpy(&d2, &d1, sizeof(int)); assert(e1 == d2); // holds, undefined/erroneous behavior assert(e2 == d2); // holds, undefined/erroneous behavior }[edit] Notes
References and const scalar objects cannot be default-initialized.
[edit] 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() { [[maybe_unused]] int n; // non-class, the value is indeterminate std::string s; // class, calls default constructor, the value is "" 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 constructor [[maybe_unused]] T1 t1; // class, calls implicit default constructor const T2 t2; // const class, calls the user-provided default constructor // t2.mem is default-initialized }[edit] 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 178 C++98 there was no value-initialization;RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4