Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/value initialization"

From cppreference.com
< cpp‎ | language
m
(Defect reports: +)
Line 55: Line 55:
 
All standard containers ({{lc|std::vector}}, {{lc|std::list}}, etc.) value-initialize their elements when constructed with a single {{tt|size_type}} argument or when grown by a call to {{c|resize()}}, unless their allocator customizes the behavior of {{c|construct}}.
 
All standard containers ({{lc|std::vector}}, {{lc|std::list}}, etc.) value-initialize their elements when constructed with a single {{tt|size_type}} argument or when grown by a call to {{c|resize()}}, unless their allocator customizes the behavior of {{c|construct}}.
  
Since C++11, value-initializing a class without a user-provided constructor, which has a member of a class type with a user-provided constructor zeroes out the member before calling its constructor:
+
Since DR 543, value-initializing a class without a user-provided constructor, which has a member of a class type with a user-provided constructor zeroes out the member before calling its constructor:
  
 
{{source|1=
 
{{source|1=
Line 157: Line 157:
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
{{dr list item|wg=cwg|dr=178|std=C++98|before=there's no value-initialization; empty parentheses invoke default-init|after=empty parentheses invoke value-init}}
+
{{dr list item|wg=cwg|dr=178|std=C++98|before=there's no value-initialization; empty initializer invoke default-init|after=empty initializer invoke value-init}}
 
{{dr list item|wg=cwg|dr=1301|std=C++11|before=defaulted default constructor skipped zero-init before construction|after=zero-init performed}}
 
{{dr list item|wg=cwg|dr=1301|std=C++11|before=defaulted default constructor skipped zero-init before construction|after=zero-init performed}}
 +
{{dr list item|wg=cwg|dr=543|std=C++98|before=value-init for a class object without any user-provided constructors<br>is equivalent to value-initializing each subobject|after=zero-initializes the object, then calls the default ctor}}
 +
{{dr list item|wg=cwg|dr=1368|std=C++98|before=any user-provided constructor causes zero-init to be skipped|after=only a user-provided default constructor skips zero-init}}
 +
{{dr list item|wg=cwg|dr=1507|std=C++98|before=value-init for a class object without any user-provided constructors<br>does not check the validity of the default ctor when the latter is trivial|after=the validity of trivial default ctor is checked}}
 +
{{dr list item|wg=cwg|dr=1502|std=C++11|before=value-initializing a union without a user-provided default ctor<br>only zero-initializes the object, despite default member initializers|after=performs default-init after zero-init}}
 
{{dr list end}}
 
{{dr list end}}
  

Revision as of 23:46, 2 November 2021

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

This is the initialization performed when an object is constructed with an empty initializer.

Contents

Syntax

T() (1)
new T () (2)
Class::Class(...) : member() { ... } (3)
T object {}; (4) (since C++11)
T{} (5) (since C++11)
new T {} (6) (since C++11)
Class::Class(...) : member{} { ... } (7) (since C++11)

Explanation

Value initialization is performed in these situations:

1,5) when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses or braces(since C++11);
2,6) when an object with dynamic storage duration is created by a new-expression with the initializer consisting of an empty pair of parentheses or braces(since C++11);
3,7) when a non-static data member or a base class is initialized using a member initializer with an empty pair of parentheses or braces(since C++11);
4) when a named object (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces.
(since C++11)

In all cases, if the empty pair of braces {} is used and T is an aggregate type, aggregate-initialization is performed instead of value-initialization.

If T is a class type that has no default constructor but has a constructor taking std::initializer_list, list-initialization is performed.

(since C++11)

The effects of value initialization are:

1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called;
2) if T is a non-union class type without any user-provided constructors, every non-static data member and base-class component of T is value-initialized;
(until C++11)
1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
however, all known compilers come into case (2) when a non-deleted defaulted default constructor is selected by overload resolution, even if there is a user-provided default constructor, see Notes;
(since C++11)
3) if T is an array type, each element of the array is value-initialized;
4) otherwise, the object is zero-initialized.

Notes

A constructor is user-provided if it is user-declared and not explicitly defaulted on its first declaration.

The syntax T object(); does not initialize an object; it declares a function that takes no arguments and returns T. The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case.

In C++98 prior to C++03 (which introduced value initialization), the expression new T() was classified as default initialization and specified zero-initialization.

References cannot be value-initialized.

As described in functional cast, the syntax T() (1) is prohibited for arrays, while T{} (5) is allowed.

All standard containers (std::vector, std::list, etc.) value-initialize their elements when constructed with a single size_type argument or when grown by a call to resize(), unless their allocator customizes the behavior of construct.

Since DR 543, value-initializing a class without a user-provided constructor, which has a member of a class type with a user-provided constructor zeroes out the member before calling its constructor:

struct A
{
    int i;
    A() { } // user-provided default ctor, does not initialize i
};
 
struct B { A a; }; // implicitly-defined default ctor
 
std::cout << B().a.i << '\n'; // value-initializes a B temporary
                              // leaves b.a.i uninitialized in C++03
                              // sets b.a.i to zero in C++11
// (note that B{}.a.i leaves b.a.i uninitialized in C++11, but for 
// a different reason: in post-DR1301 C++11, B{} is aggregate-initialization,
// which then value-initializes A, which has a user-provided ctor)

The standard specifies that zero-initialization is not performed when the class has a user-provided or deleted default constructor, even if that default constructor is not selected by overload resolution. All known compilers performs additional zero-initialization if a non-deleted defaulted default constructor is selected.

struct A {
    A() = default;
    template<class = void>
    A(int = 0) {} // A has a user-provided default constructor, which is not selected
    int x;
};
 
constexpr int test(A a) {
    return a.x; // the behavior is undefined if a's value is indeterminate
}
 
constexpr int zero = test(A());
// ill-formed: the parameter is not zero-initialized according to the standard,
// which results in undefined behavior that makes the program ill-formed in contexts 
// where constant evaluation is required.
// However, such code is accepted by all known compilers.
 
void f() {
    A a = A(); // not zero-initialized according to the standard
               // but implementations generate code for zero-initialization nonetheless
}

Example

#include <string>
#include <vector>
#include <iostream>
 
struct T1
{
    int mem1;
    std::string mem2;
}; // implicit default constructor
 
struct T2
{
    int mem1;
    std::string mem2;
    T2(const T2&) { } // user-provided copy constructor
};                    // no default constructor
 
struct T3
{
    int mem1;
    std::string mem2;
    T3() { } // user-provided default constructor
};
 
std::string s{}; // class => default-initialization, the value is ""
 
int main()
{
    int n{};                // scalar => zero-initialization, the value is 0
    double f = double();    // scalar => zero-initialization, the value is 0.0
    int* a = new int[10](); // array => value-initialization of each element
                            //          the value of each element is 0
    T1 t1{};                // class with implicit default constructor =>
                            //     t1.mem1 is zero-initialized, the value is 0
                            //     t1.mem2 is default-initialized, the value is ""
//  T2 t2{};                // error: class with no default constructor
    T3 t3{};                // class with user-provided default constructor =>
                            //     t3.mem1 is default-initialized to indeterminate value
                            //     t3.mem2 is default-initialized, the value is ""
    std::vector<int> v(3);  // value-initialization of each element
                            // the value of each element is 0
    std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n';
    std::cout << t1.mem1 << ' ' << t3.mem1 << '\n';
    delete[] a;
}

Possible output:

0 0 0 0 0
0 4199376

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's no value-initialization; empty initializer invoke default-init empty initializer invoke value-init
CWG 1301 C++11 defaulted default constructor skipped zero-init before construction zero-init performed
CWG 543 C++98 value-init for a class object without any user-provided constructors
is equivalent to value-initializing each subobject
zero-initializes the object, then calls the default ctor
CWG 1368 C++98 any user-provided constructor causes zero-init to be skipped only a user-provided default constructor skips zero-init
CWG 1507 C++98 value-init for a class object without any user-provided constructors
does not check the validity of the default ctor when the latter is trivial
the validity of trivial default ctor is checked
CWG 1502 C++11 value-initializing a union without a user-provided default ctor
only zero-initializes the object, despite default member initializers
performs default-init after zero-init

See also