Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Designated initializers (C++20)

Reference for C++20 designated initializers: initializing aggregate members by name with .member = value, ordering rules, defaults, nesting and differences from C.

A designated initializer names the member it initializes: Config c{.port = 8080, .debug = true};. It was added to C++ in C++20 for aggregate types and makes call sites self-documenting, especially for structs with several members of the same type.

The rules are stricter than in C. The designators must appear in the same order as the member declarations, you cannot mix designated and positional initializers, and array designators and nested short forms (.a.b = 1) are not allowed. Members you skip are initialized from their default member initializer, or value-initialized (zero) if they have none.

C++
struct Size { int width; int height; };
Size s{.width = 640, .height = 480};

Try it

C++ Named members and defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Nesting and order rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
T obj{ .member1 = expr1, .member2 = expr2 };
T obj{ .member1{ expr1 }, .member2{ expr2 } };   // brace form after the designator
T obj = { .member1 = expr1 };
Part Description
T Must be an aggregate: no user-declared constructors, no private or protected non-static data members, no virtual functions or virtual/private/protected base classes.
.member An identifier naming a direct non-static data member of T.
= expr or { ... } The initializer, with the same narrowing rules as brace initialization.

Errors: designators out of declaration order, mixing designated and positional initializers in the same list, naming a nonexistent member, repeating a member, and using nested designators such as .a.b = 1 or array designators [2] = 5 are all errors in C++ (several are allowed in C and as GCC extensions in C++, with a warning).

Usage notes

  • Order is the declaration order. Because evaluation of the expressions follows the order written, requiring declaration order avoids the surprise of initialization order differing from the text. Reordering the struct's members therefore breaks existing initializers, so reorder carefully.
  • Skipped members use the default member initializer if there is one, otherwise value-initialization (zero for built-ins). GCC may warn with -Wmissing-field-initializers for aggregates without defaults.
  • Anonymous unions can be initialized through one designator, but a designated initializer may initialize at most one member of a union.
  • Public interfaces. They shine for "options" structs passed to functions; a call like connect({.host = h, .port = 8080}) is self-describing and keeps working when new defaulted members are added at the end.
  • Aggregates only. Adding a constructor to the type disables designated initializers; there is no equivalent for constructor parameters (C++ has no named arguments).
  • Inheritance. Since C++17 a derived class can be an aggregate, but you cannot designate base-class subobjects; the base is initialized positionally first.
  • Difference from C99. C allows any order, nested designators, array designators and mixing. C++20 intentionally keeps only the safe subset. Code that compiles as C may not compile as C++.
  • Version note: standard since C++20; GCC accepts the syntax earlier as an extension with a pedantic warning.