default initialization

From cppreference.com
< cpp‎ | language

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

Contents

[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 duration is declared with no initializer
2) when an object with dynamic storage duration is created by a new-expression without an initializer
3) 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 class type, the default constructor 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, nothing is done.

If T is a const-qualified type, it must be a class type with a user-provided default constructor.

Use of an indeterminate value obtained by default-initializing a non-class variable of any type is undefined behavior, except in the following cases:

  • if the indeterminate value of unsigned narrow character type is assigned to another variable with unsigned narrow character type (the value of the variable becomes indeterminate, but the behavior is not undefined)
  • if the indeterminate value of unsigned narrow character type is used to initialize another variable with unsigned narrow character type
  • if the indeterminate value of unsigned narrow character type results from
  • the second or third operand of a conditional expression
  • the right operand of the comma operator
  • the operand of a cast or conversion to an unsigned narrow character type
  • a discarded-value expression
int f(bool b)
{
    int x; // value of x is indeterminate
    int y = x; // undefined behavior
    unsigned char c; // value of c is indeterminate
    unsigned char d = c; // OK, value of d is indeterminate
    int e = d; // undefined behavior
    return b ? d : 0; // undefined behavior if b is true
}
(since C++14)

[edit] Notes

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)

Reference cannot be default-initialized.

[edit] Example

#include <string>
struct T1 {};
class T2 { 
    int mem;
 public:
    T2() {} // "mem" not in initializer list
};
int n; // A two-phase initialization is done
       // In the first phase, zero initialization initializes n to zero
       // In the second phase, default initialization does nothing, leaving n being zero
int main()
{
    int n;            // non-class: the value is undeterminate
    std::string s;    // calls default ctor, the value is "" (empty string)
    std::string a[2]; // calls default ctor, creates two empty strings
//    int& r;         // error: default-initializing a reference
//    const int n;    // error: const non-class type
//    const T1 nd;    // error: const class type with implicit ctor
    T1 t1; // ok, calls implicit default ctor
    const T2 t2; // ok, calls the user-provided default ctor 
                 // t2.mem is default-initialized (to indeterminate value)
}


[edit] See also