[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 int | int() → 0 | int{} → 0 |
| Narrowing | int(3.14) allowed | int{3.14} usually error |
| MVP | T f(); may be a function | T f{} is an object |
vector | vector<int>(10,0) size/value | vector<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/falseas 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
| Form | Local int | Class type |
|---|---|---|
Default int x; | Indeterminate | Default ctor |
Value int x{} | 0 | Default ctor |
Direct int x(10) | 10 | Matching ctor |
Performance
Zeroing large arrays costs CPU. If you immediately overwrite everything, default init plus assignment may win—measure.