""" Default-initialization C++ C++ language Initialization This is the initialization…
- User
""" Default-initialization C++ C++ language Initialization This is the initialization performed when an object is constructed with no initializer.
Syntax T object ; (1) new T (2) Explanation Default-initialization is performed in three situations:
- when a variable with automatic, static, or thread-local storage duration is declared with no initializer;
- when an object with dynamic storage duration is created by a new-expression with no initializer;
- when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called. The effects of default-initialization are:
if 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; if T is an array type, every element of the array is default-initialized; otherwise, no initialization is performed (see notes). 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
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) each direct non-static data member M of T is of class type X (or array thereof), X is const-default-constructible, and T has no direct variant members, and (until C++11) each direct non-variant non-static data member M of T has a default member initializer or, if M is of class type X (or array thereof), X is const-default-constructible, if T is a union with at least one non-static data member, exactly one variant member has a default member initializer, if 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 (since C++11) each potentially constructed base class of T is const-default-constructible.
Indeterminate and erroneous values 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:
- Assistant