value initialization

From cppreference.com
< cpp‎ | language

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

Contents

[edit] 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)

[edit] 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).
4) when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of 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)

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.


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.
(until C++11)
1) If T is a class type with no default constructor or with a user-provided default constructor or with a deleted default constructor, the object is default-initialized.
(since C++11)
2) If T is an non-union class type without any user-provided constructors, then every non-static data member and base-class component of T is value-initialized
(until C++11)
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 a defaulted default constructor or with an implicitly-defined one) then the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor
(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.

[edit] 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.

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().

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:

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)

[edit] Example

#include <string>
#include <vector>
#include <iostream>
 
struct T1 {
    int mem1;
    std::string mem2;
}; // no constructors
struct T2 { 
    int mem1;
    std::string mem2;
    T2(const T2&) {} // a constructor, but no default
};
struct T3 { 
    int mem1;
    std::string mem2;
    T3() {} // user-provided default ctor
};
 
std::string s{}; // calls default ctor, the value is "" (empty string)
int main()
{
    int n{};     // non-class value-initialization, value is 0
    double f = double(); // non-class value-init, value is 0.0
    int* a = new int[10](); // array of 10 zeroes
 
    T1 t1{}; // no ctors: zero-initialized
             // t1.mem1 is zero-initialized
             // t1.mem2 is default-initialized
//    T2 t2{}; // error: has a ctor, but no default ctor
    T3 t3{}; // user-defined default ctor:
             // t3.mem1 is default-initialized (the value is indeterminate)
             // t3.mem2 is default-initialized
 
    std::vector<int> v(3); // value-initializes three ints
 
    std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n';
    std::cout << t1.mem1 << ' ' << t3.mem1 << '\n';
    delete[] a;
}

Output:

0 0 0 0 0
0 4199376

[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 1301 C++11 defaulted default constructor skipped zero-init before construction zero-init performed

[edit] See also