[2026] C++ Value Initialization | Empty `{}` and `()`

[2026] C++ Value Initialization | Empty `{}` and `()`

이 글의 핵심

Value initialization uses empty `()` or `{}`. Scalars become zero-like; classes call the default constructor. Differs from default initialization for locals; compares with zero initialization.

What is value initialization?

Value initialization happens when you use empty {} or () to construct an object. For scalars you get zero-like values; for class types the default constructor runs.

int x{};     // 0
int* ptr{};  // nullptr
double d{};  // 0.0

Why it matters: Default initialization can leave indeterminate values for local scalars; value initialization gives safe defaults.

int x;   // indeterminate (local)
int y{}; // 0

When does it happen?

  • T{}, T{ } — list form with no arguments → value init rules for the object.
  • T() temporaries, new T() — value initialization.
  • T t; with no initializer — default initialization, not value init. If you need zeros, do not rely on default init for locals.

{} vs () when both value-initialize

Often int() and int{} both yield 0. Differences matter for narrowing and the Most Vexing Parse:

Topic(){}
Value-init intint() → 0int{} → 0
Narrowingint(3.14) allowedint{3.14} usually error
MVPT f(); may be a functionT f{} is an object
vectorvector<int>(10,0) size/valuevector<int>{10,0} two elements
아래 코드는 cpp를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
Widget w2{};     // value init (default ctor)
// Widget w3();  // MVP: function declaration
auto p2 = new double();  // 0.0
auto p3 = new double{};  // 0.0

Scalars vs classes

  • Scalars: zero / nullptr / false as appropriate.
  • Classes: default ctor must exist and be callable.
  • Aggregates: T{} often combines with zero and aggregate rules. 아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며, 에러 처리를 통해 안정성을 확보합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
struct Pod { int x; int y; };
Pod p{};  // members zeroed per rules
class NonDefault {
    NonDefault(int) {}
};
// NonDefault nd{};  // error: no default ctor

Arrays and new

아래 코드는 cpp를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

int arr[5]{};   // all zero
int* p1 = new int();   // 0
int* p2 = new int{};   // 0
int* a1 = new int[5]();  // all zero
int* a2 = new int[5]{};  // all zero

Member initialization

아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 코드를 직접 실행해보면서 동작을 확인해보세요.

class Data {
    int x{};
    double y{};
    int* ptr{};
public:
    Data() = default;
};

Comparison table

FormLocal intClass type
Default int x;IndeterminateDefault ctor
Value int x{}0Default ctor
Direct int x(10)10Matching ctor

Performance

Zeroing large arrays costs CPU. If you immediately overwrite everything, default init plus assignment may win—measure.

FAQ

Q1: Definition?
A: {} or () empty initialization—zeros for scalars, default ctor for classes. Q2: vs default initialization?
A: int x; can be garbage locally; int x{} is zero. Q3: Performance?
A: Zero-fill has cost; choose consciously for huge buffers. Q4: {} vs ()?
A: Mostly same for value-init; {} catches narrowing. Related: Zero initialization, Default initialization, List initialization. One line: Value initialization is the safe “default value” path—especially T{} for locals.

Keywords

C++, value initialization, default initialization, zero initialization, braced init.

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3