C++ Value Initialization | Empty `{}` and `()`
이 글의 핵심
Value initialization applies when you write `T{}`, `T()`, or `new T()`. It gives safe defaults for scalars and default-constructs class types—unlike leaving locals uninitialized.
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 |
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.
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
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
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.
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.
Related posts
- C++ Zero initialization
- C++ Default initialization
- C++ List initialization
Keywords
C++, value initialization, default initialization, zero initialization, braced init.
More links
- C++ Aggregate initialization
- C++ Copy initialization