# Initialization Forms in C++

> Decode default, value, zero, direct, copy, aggregate, and list initialization—and prevent the narrowing, most-vexing-parse, and initializer_list traps.

- **Source:** C++ Atlas (https://cppatlas.com/topics/initialization-forms)
- **Track:** Types and initialization
- **Estimated reading time:** 9 minutes
- **ISO C++ clauses:** [dcl.init.general] (https://eel.is/c++draft/dcl.init.general), [dcl.init.aggr] (https://eel.is/c++draft/dcl.init.aggr), [dcl.init.list] (https://eel.is/c++draft/dcl.init.list), [over.match.list] (https://eel.is/c++draft/over.match.list)
- **cppreference links:** [Initialization](https://en.cppreference.com/w/cpp/language/initialization), [Default-initialization](https://en.cppreference.com/w/cpp/language/default_initialization), [Value-initialization](https://en.cppreference.com/w/cpp/language/value_initialization), [Zero-initialization](https://en.cppreference.com/w/cpp/language/zero_initialization), [Direct-initialization](https://en.cppreference.com/w/cpp/language/direct_initialization), [Copy-initialization](https://en.cppreference.com/w/cpp/language/copy_initialization), [List-initialization](https://en.cppreference.com/w/cpp/language/list_initialization), [Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization)
- **Prerequisites:** translation-units-and-linkage

Few languages offer as many ways to initialize a variable as C++: `int x;`, `int x{};`, `int x = 0;`, `int x(0);`, and `int x = {0};` look almost interchangeable on scalars, yet invoke completely distinct rules on classes, arrays, and containers. Knowing which syntax activates which initialization algorithm removes guesswork and eliminates silent bugs.

## The syntactic map of initialization

Under `[dcl.init.general]`, C++ selects an initialization algorithm purely from declaration syntax before checking constructors or conversions. Each form decides whether scalar bytes start zeroed, whether `explicit` constructors are eligible, and whether narrowing conversions are allowed.

| Form | Syntax example | Zeroes unmentioned scalars? | Allows `explicit` ctors? | Rejects narrowing? |
| --- | --- | --- | --- | --- |
| Default-initialization | `T x;` | No (indeterminate on stack) | Calls default ctor | Not applicable |
| Value-initialization | `T x{};` or `T()` | Yes, unless user-provided ctor | Calls default ctor | Not applicable |
| Zero-initialization | Static/thread-local baseline | Yes, recursively | No ctor invoked | Not applicable |
| Direct-initialization | `T x(a, b);` | Via chosen ctor | Yes | No |
| Copy-initialization | `T x = expr;` | Via chosen ctor | No | No |
| Direct-list-initialization | `T x{a, b};` | Empty members zeroed | Yes | Yes |
| Copy-list-initialization | `T x = {a, b};` | Empty members zeroed | No | Yes |
| Aggregate initialization | `Aggr p{.x = 1};` | Unmentioned fields `{}`-initialized | No user ctors allowed | Yes (for braced form) |

> **Mental model.** Syntax picks the ruleset first: parentheses choose direct-initialization, `=` chooses copy-initialization, braces choose list-initialization, and omitting an initializer chooses default-initialization.

## Default, value, and zero initialization

Writing `int n;` inside a function performs **default-initialization** (`[dcl.init.general]`): for non-class scalars with automatic storage duration, no initialization occurs at all, leaving `n` with an indeterminate value whose read causes Undefined Behavior (reclassified as erroneous behavior in C++26). For class types, default-initialization invokes the default constructor.

Writing `T x{};` or `T()` performs **value-initialization**. If `T` is a scalar, it is zero-initialized. If `T` is a class *without* a user-provided or deleted default constructor (including aggregates and classes with `T() = default;` declared inside the class body only when defaulted—wait carefully on that boundary: specifically, if the default constructor is neither user-provided nor deleted, the object is **zero-initialized first** and then default-initialized!). If `T` defines a user-provided default constructor `T() {}`, zero-initialization is skipped and any unmentioned scalar member remains indeterminate.

Meanwhile, **zero-initialization** applies automatically before any dynamic initialization to all variables with static or thread-local storage duration, guaranteeing deterministic starting bytes at program or thread startup.

```cpp
struct Plain {
    int count; // No user-provided constructor
};

struct Custom {
    int count;
    Custom() {} // User-provided constructor: skips pre-zeroing!
};

int check_zeroing() {
    Plain p{};  // Value-initialized: zero-initialized first -> p.count == 0
    Custom c{}; // Value-initialized: calls Custom::Custom(), c.count is indeterminate!
    (void)c;
    return p.count;
}
```

## Direct versus copy initialization

**Direct-initialization** (`T x(args);` or `static_cast<T>(expr)`) considers *all* constructors of `T`, including those marked `explicit`. **Copy-initialization** (`T x = expr;`, passing by value `f(arg)`, or `return expr;`) considers only non-`explicit` converting constructors and non-`explicit` conversion operators (`[dcl.init.general]`).

Since C++17 guaranteed copy elision, `T x = T(arg);` no longer requires an accessible copy or move constructor—the prvalue `T(arg)` initializes `x` directly in place—yet `T x = arg;` still rejects an `explicit` constructor taking `arg`.

One syntax hazard haunts direct-initialization with empty arguments: the **most vexing parse**. Because anything that *can* be parsed as a function declaration *must* be parsed as one, `Widget w();` declares a zero-argument function returning `Widget`, not a default-initialized object! Braces (`Widget w{};`) never parse as a function declaration and cleanly request value-initialization.

```cpp
struct Port {
    explicit Port(int p) : value(p) {}
    int value;
};

void check_explicit() {
    Port a(8080);   // Direct-init: explicit ctor allowed
    Port b{443};    // Direct-list-init: explicit ctor allowed
    // Port c = 80; // Error: copy-init cannot use explicit Port(int)
    // Port d = {80}; // Error: copy-list-init cannot use explicit Port(int)
    (void)a;
    (void)b;
}
```

## List initialization, narrowing, and greedy initializer_list

Braced initialization (`{...}`)—**list-initialization** (`[dcl.init.list]`)—solves the most vexing parse and enforces a vital safety rule: **narrowing conversions are ill-formed**. Converting `double` to `int`, or `int` to `char` when the source is not a constant expression whose value fits in `char`, fails to compile.

Yet list-initialization carries two infamous traps:

1. **Copy-list-initialization still checks `explicit`.** `Port p = {80};` fails to compile whenever `Port(int)` is `explicit`.
2. **Greedy `std::initializer_list` constructors.** Under `[over.match.list]`, braced overload resolution runs in two phases: first, the compiler searches *only* constructors taking `std::initializer_list`. Normal constructors are examined only if **no** viable `initializer_list` constructor exists (or if the braces are empty `{}`, which prefers the default constructor).

> **Standardese.** Under `[over.match.list]`, `T{...}` with a non-empty braced-init-list resolves in two stages: stage one considers exclusively `std::initializer_list` constructors; only when none is viable does stage two consider all constructors of `T`.

```cpp
#include <vector>

void check_vector_braces() {
    std::vector<int> five_ones(5, 1);  // Size 5: {1, 1, 1, 1, 1} (normal ctor)
    std::vector<int> two_elems{5, 1};  // Size 2: {5, 1} (greedy initializer_list!)
    std::vector<int> empty_vec{};      // Size 0: empty {} picks default ctor

    constexpr int small = 65;
    char ok_char{small};               // Valid: constant expression 65 fits in char
    // int bad{3.14};                  // Error: narrowing conversion from double to int
    (void)five_ones;
    (void)two_elems;
    (void)empty_vec;
    (void)ok_char;
}
```

> **Pitfall.** Writing `std::vector<int> v{10, 2};` constructs a 2-element vector `[10, 2]`, not ten copies of `2`. Always use parentheses `v(10, 2)` when passing element-count or capacity arguments to types that also offer an `initializer_list` constructor.

## Aggregates and C++20 changes

An **aggregate** (`[dcl.init.aggr]`) is an array or a simple class with no private or protected direct non-static data members, no virtual functions, no virtual/private/protected base classes, and—crucially in C++20—**no user-declared or inherited constructors**. Prior to C++20, writing `S() = default;` kept `S` an aggregate because the constructor was not user-provided; C++20 closed that loophole, making `S{42}` ill-formed whenever *any* constructor is explicitly declared (`= default` or `= delete` included).

C++20 also added two expressive capabilities for aggregates:

- **Designated initializers:** `Config cfg{.port = 8080, .tls = true};` initializes named members in exact declaration order. Out-of-order designators, mixing positional and designated elements, nested `.a.b`, and array `[0]` designators are ill-formed in standard C++. Unmentioned members are initialized from their default member initializers or `{}`.
- **Parenthesized aggregate initialization:** `Point p(1, 2);` works for aggregates in C++20 (without narrowing rejection), finally enabling `std::make_unique<Point>(1, 2)` and `emplace_back(1, 2)` without manually writing a constructor.

> **Pitfall.** Adding `MyPod() = delete;` or `MyPod() = default;` inside a struct in C++20 removes its aggregate status (`std::is_aggregate_v<MyPod>` becomes `false`), breaking `MyPod{1, 2}` aggregate initialization for every caller.

```cpp
#include <memory>
#include <type_traits>

struct Opts {
    int port = 80;
    bool tls = false;
};

struct NotAggregate {
    NotAggregate() = default; // User-declared ctor removes aggregate status in C++20+
    int x;
};

static_assert(std::is_aggregate_v<Opts>);
static_assert(!std::is_aggregate_v<NotAggregate>);

void check_cpp20_aggregates() {
    Opts o{.tls = true};                 // Valid: .port gets default 80, .tls becomes true
    auto p = std::make_unique<Opts>(443, true); // C++20 parenthesized aggregate init
    (void)o;
    (void)p;
}
```

## What to remember

- Declaration syntax dictates the initialization algorithm before constructor lookup runs (`T x;` default, `T x{};` value, `T x(a);` direct, `T x = a;` copy, `T x{a};` direct-list).
- Copy-initialization (`= expr` and `= {expr}`) rejects `explicit` constructors; direct forms (`(...)` and `{...}`) allow them.
- Braced list-initialization forbids narrowing conversions and avoids the most vexing parse, yet prefers `std::initializer_list` constructors whenever non-empty braces match.
- Value-initialization zeroes non-user-provided classes before calling defaulted constructors, whereas a user-provided `T() {}` skips zeroing unmentioned scalar fields.
- In C++20+, any user-declared constructor (`= default` or `= delete` included) disqualifies a class from being an aggregate, while true aggregates support ordered `.member = val` designated initializers and `(args...)` initialization.

## Concept Map Outline

- **Initialization Forms:** Declaration syntax picks the C++ initialization algorithm before constructor overload resolution runs.
  - **Default, Value & Zero:** Controls whether scalar fields start uninitialized or deterministically zeroed.
    - **Default-Initialization:** Leaves stack scalars indeterminate and invokes default constructors on classes.
    - **Value-Initialization:** Zeroes scalars and defaulted classes without user-provided constructors before calling ctor.
    - **Zero-Initialization:** Clears static and thread-local storage before any dynamic initialization executes.
  - **Direct vs. Copy:** Separates explicit constructor eligibility and resolves the most vexing parse.
    - **Direct-Initialization:** Syntax T x(args) considers all constructors including explicit ones.
    - **Copy-Initialization:** Syntax T x = expr skips explicit constructors and conversion functions.
    - **Most Vexing Parse:** Widget w(); parses as a function declaration rather than a default-initialized variable.
  - **List Initialization:** Braced syntax prevents narrowing and function-parse ambiguity while adding two-phase lookup.
    - **Narrowing Prohibition:** Rejects lossy conversions unless converting a constant expression whose value fits.
    - **Greedy initializer_list:** Non-empty braces strongly prefer std::initializer_list constructors over normal overloads.
    - **Empty Braces Rule:** Empty T x{} selects the default constructor rather than an empty initializer_list.
  - **Aggregate Initialization:** Initializes public-member arrays and constructor-free structs directly in declaration order.
    - **C++20 Aggregate Rule:** Any user-declared constructor including = default or = delete disqualifies aggregate status.
    - **Designated Initializers:** Syntax {.field = val} initializes named aggregate members strictly in declaration order.
    - **Parenthesized Aggregates:** C++20 allows T(a, b) for aggregates so emplace and make_unique work seamlessly.

## Standardese Quiz Questions

### Question 1 [warmup] ([stmt.ambig], [dcl.init.general])

Why does `Widget w();` inside a function fail when you subsequently call `w.run()`?

A. `Widget w();` declares a function named `w` taking no arguments and returning `Widget`, not a variable.
B. `Widget` has no copy constructor.
C. Parentheses can only initialize fundamental types, not classes.
D. `Widget w();` value-initializes `w` as `const`.

**Answer (A):** `Widget w();` declares a function named `w` taking no arguments and returning `Widget`, not a variable.

**Why:** Under C++'s ambiguity resolution (the most vexing parse, [stmt.ambig]), any statement that can be parsed as a declaration is parsed as a declaration—so `Widget w();` declares a function `w()`. Using `Widget w{};` or `Widget w;` constructs an object.

### Question 2 [warmup] ([over.match.list], [dcl.init.list])

Given `std::vector<int> a(4, 9);` and `std::vector<int> b{4, 9};`, what are the sizes of `a` and `b`?

A. `a` has size 2; `b` has size 4.
B. Both `a` and `b` have size 2.
C. `a` has size 4 (four 9s); `b` has size 2 (elements 4 and 9).
D. Both `a` and `b` have size 4.

**Answer (C):** `a` has size 4 (four 9s); `b` has size 2 (elements 4 and 9).

**Why:** Parentheses `a(4, 9)` invoke `std::vector(size_type count, const int& value)`, creating 4 copies of 9. Braces `b{4, 9}` trigger `[over.match.list]`, strongly preferring `std::vector(std::initializer_list<int>)` and creating 2 elements.

### Question 3 [tricky] ([dcl.init.general], [over.match.list])

Which initialization is well-formed in C++20/23 given `struct Box { explicit Box(int) {} };`?

```cpp
struct Box {
    explicit Box(int) {}
};
```

A. `Box b = 5;`
B. `Box b = {5};`
C. `void f(Box); f({5});`
D. `Box b{5};`

**Answer (D):** `Box b{5};`

**Why:** `Box b{5};` is direct-list-initialization, which allows `explicit` constructors. All three other forms (`= 5`, `= {5}`, and passing `{5}` to a parameter) are copy-initialization or copy-list-initialization, which reject `explicit` constructors under [over.match.list].

### Question 4 [tricky] ([dcl.init.general])

What is the value of `a.x` and `b.x` after running this C++23 function?

```cpp
struct A { int x; };
struct B { int x; B() {} };
void f() {
    A a{};
    B b{};
}
```

A. `a.x` is 0; `b.x` is indeterminate.
B. Both `a.x` and `b.x` are guaranteed to be 0.
C. Both `a.x` and `b.x` are indeterminate.
D. `a.x` is indeterminate; `b.x` is 0.

**Answer (A):** `a.x` is 0; `b.x` is indeterminate.

**Why:** Value-initialization (`{}`) zero-initializes `A` first because `A` has no user-provided or deleted default constructor, setting `a.x` to 0. Because `B` has a user-provided default constructor `B() {}`, value-initialization skips zero-initialization and directly runs `B()`, leaving `b.x` indeterminate.

### Question 5 [standardese] ([dcl.init.aggr])

Why does `struct S { S() = default; int x; };` fail `S s{42};` starting in C++20 even though it compiled in C++17?

A. C++20 banned `= default` on constructors inside structs.
B. C++20 changed aggregate rules from 'no user-provided constructors' to 'no user-declared constructors', disqualifying S.
C. C++20 treats `42` as narrowing when initializing `int` in an aggregate.
D. `S{42}` now calls `std::initializer_list<int>` automatically.

**Answer (B):** C++20 changed aggregate rules from 'no user-provided constructors' to 'no user-declared constructors', disqualifying S.

**Why:** In C++17, `S() = default;` inside the class body was user-declared but not user-provided, so `S` remained an aggregate. C++20 ([dcl.init.aggr]) changed the requirement to 'no user-declared or inherited constructors', so `S` is no longer an aggregate and has no constructor taking `int`.

### Question 6 [standardese] ([dcl.init.list])

Which braced conversion is NOT considered a narrowing conversion under [dcl.init.list]?

```cpp
constexpr int ok = 100;
int runtime_val = 100;
double d = 2.0;
```

A. `char c{runtime_val};`
B. `int n{d};`
C. `char c{ok};`
D. `float f{1e300};`

**Answer (C):** `char c{ok};`

**Why:** Under [dcl.init.list], converting an integer type to another integer type is NOT narrowing if the source is a constant expression whose value can be represented exactly in the target type (`constexpr int ok = 100` fits in `char`). Non-constant `runtime_val`, floating-to-integer `int{d}`, and out-of-range `float{1e300}` are all ill-formed narrowing conversions.
