This is the initialization performed when an object is constructed with no initializer.
T object ; | (1) | |
new T | (2) |
Default initialization is performed in three situations:
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; 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) |
| (until C++11) |
| (since C++11) |
each potentially constructed base class of T
is const-default-constructible.
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:
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); 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); unsigned char
or std::byte
(since C++17) results from unsigned char
or std::byte
(since C++17), 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 }
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).
References and const scalar objects cannot be default-initialized.
Feature-test macro | Value | Std | Comment |
---|---|---|---|
__cpp_constexpr | 201907L | (C++20) | Trivial default initialization and asm-declaration in constexpr functions |
#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) }
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's no value-initialization; empty initializer invoke default-init (though new T() also performs zero-init) | empty initializer invoke value-init |
CWG 253 | C++98 | default initialization of a const object could not call an implicitly declared default constructor | allowed if all subobjects are initialized |
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 |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/default_initialization