skip to content

In a C++20 code review, which rules decide whether WindowConfig{.title = "Game", .width = 1920} compiles, and what do the members it omits get?

level: seniorimportance: should knowfreq 40%

answer

  1. only aggregates take designators
  2. declaration order, as a subsequence
  3. no mixing, no nesting, no array indices
  4. omitted: default member initializer, else {}
  5. any user-declared constructor ends aggregate status

basics

~10 s

It is ill-formed: C++20 designators must name members in declaration order, and title comes after width. Designated initializers work only on aggregates; omitted members use their default member initializer or are copy-initialized from {}.

solid answer

~40 s

C++20 **designated initializers** apply only to **aggregates**, and the designators must form a **subsequence of the members in declaration order**, so `.title` before `.width` is ill-formed when `width` is declared first. The list must be all-designated — no mixing with positional values — and each designator is a single `.member`: no `.window.width`, no `[0]`. Members you omit are initialized from their **default member initializer** if they have one, otherwise copy-initialized from `{}` (zero for scalars, default-constructed for classes); an omitted reference member without one makes it ill-formed. Watch the aggregate definition too: since C++20, **any user-declared constructor**, even `= default` or `= delete`, makes a class a non-aggregate, so adding `WindowConfig() = default;` breaks every designated initializer. Clang accepts out-of-order, mixed and nested forms as extensions with warnings (`-Wreorder-init-list`, `-Wc99-designator`), so test with `-pedantic-errors`.

code

cpp · 18 lines
cpp
#include <iostream>
#include <string>

struct WindowConfig {
    int width = 1280;
    int height = 720;
    std::string title;
    bool vsync = true;
};

int main() {
    WindowConfig a;                                    // default member initializers
    WindowConfig c{1920, 1080, "Game"};                // positional; vsync keeps true
    WindowConfig d{.width = 2560, .title = "Editor"};  // designated, in declaration order
    std::cout << c.width << 'x' << c.height << ' ' << c.vsync << '\n';
    std::cout << d.width << 'x' << d.height << ' ' << d.title << ' ' << d.vsync << '\n';
    std::cout << a.width << 'x' << a.height << '\n';
}

go deeper

for a junior

Know that C++20 lets you name aggregate members in braces, in the order they are declared.

for a middle

Explain the order, no-mixing and no-nesting rules, and what an omitted member is initialized with.

for a senior

Catch the C++20 aggregate-definition break from a user-declared constructor, and the Clang extensions that hide non-portable designators.

for a principal

Decide whether configuration structs stay aggregates as a rule, and which pedantic flags protect designated-initializer portability across toolchains.

## What a designated initializer is C++20 lets an aggregate be initialized by naming its members: ```cpp struct WindowConfig { int width = 1280; int height = 720; std::string title; bool vsync = true; }; WindowConfig editor{.width = 2560, .title = "Editor"}; ``` This is still **aggregate initialization**; the designators only say which element each initializer belongs to. Four rules govern it: 1. **Only aggregates.** If the braced list contains designators, the target type must be an aggregate class. 2. **Declaration order.** The designated members must form a **subsequence** of the class's direct non-static data members in declaration order. Skipping is allowed; reordering is not. `{.title = "Game", .width = 1920}` is ill-formed because `title` is declared after `width`. 3. **All or nothing.** A list is either fully positional or fully designated; `{.width = 1920, 1080}` is ill-formed. 4. **One level, members only.** A designator is `.identifier`. Nested designators (`.window.width`) and array designators (`[2] = x`), both valid in C, are not C++; write `{.window = {.width = 800}}` instead. The order rule keeps the written order and the actual order the same: C++ initializes members in declaration order, and it evaluates the initializer-clauses of a braced list left to right. ## What omitted members get For each member not explicitly initialized: - if it has a **default member initializer**, that initializer is used — `vsync` stays `true`, `height` stays 720; - otherwise it is **copy-initialized from an empty initializer list**, so an `int` becomes 0 and a `std::string` is default-constructed; - if it is a **reference** with no default member initializer, the program is ill-formed. So unlike `WindowConfig cfg;` at block scope, which leaves any member without a default member initializer indeterminate, a designated initializer never leaves a member uninitialized. ## The C++20 aggregate definition change An aggregate is an array or a class with no user-declared or inherited constructors, no private or protected direct non-static data members, no private or protected direct bases, and no virtual functions or virtual bases. The **"user-declared"** wording is new in C++20; C++17 excluded only *user-provided*, `explicit` and inherited constructors, so a defaulted or deleted constructor left a class an aggregate. The consequences, listed in the standard's compatibility annex: - `struct A { A() = delete; }; A a{};` compiled in C++17 through aggregate initialization and is ill-formed in C++20. - `struct B { B() = default; int i = 0; }; B b = {1};` is ill-formed in C++20. - In a review, a commit that adds `WindowConfig() = default;` "for clarity" breaks every designated and positional brace initialization of that struct. ## How compilers diagnose violations The standard requires a diagnostic, and Clang's is a **warning** by default, because it supports the C forms as extensions: | Code | Clang 21 diagnostic (default) | |---|---| | `{.height = 1080, .width = 1920}` | warning: ISO C++ requires field designators to be specified in declaration order [-Wreorder-init-list] | | `{.width = 1920, 1080}` | warning: mixture of designated and non-designated initializers in the same initializer list is a C99 extension [-Wc99-designator] | | `{.window.width = 800}` | warning: nested designators are a C99 extension [-Wc99-designator] | With `-pedantic-errors` all three become errors. Code that relies on the extension compiles on one toolchain and fails on a stricter one, so portable projects should build with `-pedantic-errors` or promote those warnings with `-Werror`. ## Reviewing a designated initializer A quick checklist for a pull request that builds configuration structs this way: - Are the designators in declaration order? Reordering the struct's members later breaks every call site, so member order is now part of the API. - Do omitted members have sensible default member initializers, or are they silently zeroed? - Is the struct still an aggregate after this change — no new constructor, private member or virtual function? - Does the build use `-pedantic-errors`, or at least `-Werror=reorder-init-list` and `-Werror=c99-designator` on Clang? ## Related C++20 rules worth knowing - **Parenthesized aggregate initialization** (`WindowConfig w(1920, 1080);`) was added in the same standard, but it allows no designators, permits narrowing and does not elide braces. - Designated initializers keep brace semantics: narrowing is still ill-formed, `{.width = 1920.5}` included. - Because they only work on aggregates, adding a constructor, a private member or a virtual function to a configuration struct silently changes which initialization syntax its users may write — treat those as API changes.

  • Why does C++ insist on declaration order for designators when C does not?
    C++ members are initialized in declaration order, and the initializer-clauses of a braced list are evaluated left to right. Allowing `{.b = f(), .a = g()}` would make the written order and the order of side effects disagree, and for class-type members the order of construction and destruction matters. Requiring a subsequence of declaration order keeps them identical.
  • A struct gains WindowConfig() = default; and designated initializers stop compiling. Why, and what is the fix?
    Since C++20 a class with any user-declared constructor, even a defaulted one, is not an aggregate, and designated initializers require an aggregate. The usual fix is to delete the defaulted constructor and give members default member initializers instead; they provide the same defaults while keeping aggregate status.

  • designators can appear in any order, as in C
  • omitted members are left uninitialized
  • a defaulted constructor keeps a struct an aggregate in C++20
  • nested designators like .window.width are standard C++20
  • if Clang accepts it, the designated initializer is portable

Assumes C++20 or later: designated initializers, the user-declared-constructor aggregate rule and parenthesized aggregate initialization all arrived in C++20 and are unchanged in C++23.