이름공간
변수
행위

기본 초기화

cppreference.com
< cpp‎ | language

기본 초기화는 이니셜라이저 없이 변수가 생성될 때 수행됩니다.

목차

[편집] Syntax

T object ; (1)
new T ;

new T ( ) ; (until c++03)

(2)

[편집] Explanation

기본 초기화는 아래 세가지 상황에서 수행됩니다:

1) automatic, static, 또는 thread-local 저장소 기간인 변수가 이니셜라이저 없이 선언될 때;
2) 오브젝트가 이니셜라이저 없는 new-표현식을 통해 동적 저장소 기간으로 생성될 때또는 오브젝트가 빈 괄호쌍으로 이루어진 이니셜라이저를 가진 new-표현식 통해 생성될 때 (until C++03);
3) 기본 클래스나 non-static 데이터 멤버가 생성자 초기화 리스트에 없고, 해당 생성자가 호출될 때.

The effects of default initialization are:

  • if T is a 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, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

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)

Prior to C++03 (which introduced value initialization), the expression new T() as well as a member initializer naming a base or a member with the initializer in the form of an empty pair of parentheses was classified as default initialization, but specified zero initialization for non-class types.

(until C++03)


If T is a cv-qualified type, its cv-unqualified version is used for the purpose of default-initialization.

(until C++11)

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:

  • if the indeterminate value of unsigned narrow character type or std::byte is assigned to another variable with unsigned narrow character type or std::byte (the value of the variable becomes indeterminate, but the behavior is not undefined);
  • if the indeterminate value of unsigned narrow character type or std::byte is used to initialize another variable with unsigned narrow character type or std::byte;
  • if the indeterminate value of unsigned narrow character type or std::byte 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 or std::byte,
  • a discarded-value expression.
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
}
(since C++14)

[편집] 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)

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

Reference cannot be default-initialized.

[편집] Example

#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)
}

[편집] See also