C++20 designated initializers

The C++20 standard provides new ways to initialize aggregates. These are:

  • list initialization with designated initializers, that has the following forms:
  • direct initialization, that has the following form:

In this article, we will see how list initialization with designated initializers work.

The designated initialization syntax allows to initialize non-static direct data members of a type T. Here is an example:

The class foo has two non-static data members, a and c. When initializing the object f, the member a is initialized with the syntax .a = 42. In this context, .a is called a designator.

The following rules apply to designated initializers:

  • a designator must refer to a non-static direct data member
  • all the designator used in the initialization expression must follow the order of the declaration of the data members in the class
  • not all data members must have a designator, but those that do must follow the rule above
  • it is not possible to mix designated and non-designated initialization
  • desginators of the same data member cannot appear multiple times
  • designators cannot be nested

Let us see several examples to understand it better. Consider the following classes:

The following inialization is allowed:

However, the following forms of initialization are illegal:

Here are several more examples. Consider the following classes and functions:

The following calls are permitted:

However, the following calls are, on the other hand, erroneous:

A designated initializer, and only one, can be used to initialize a union. Let us consider the following union type:

The following forms of initialization are correct:

However, having more than one designator is not allowed:

Designated initialization is a feature that is also available in the C programming language. However, it is more relaxed than in C++. In C, it is possible to perform out-of-order designated initialization, to mix designated initializers and regular initializers, to nest designators, and to initialize arrays with designators. Therefore, in this aspect, C and C++ are not fully compatible.

Designated initializers are supported in VC++ 2019 169.1, GCC 8 and Clang 10.

4 Replies to “C++20 designated initializers”

  1. foo f6{ .d = 1, .a = 42 }; // ERROR: out-of-order

    f({.a = 1, .b = 2}); // ERROR: ambiguous between f(A) and f(B)

    This is a contradiction. If the order is strict, the 2nd one is not ambigous and it can only be A (as B would be out of order).

    Was it a mistake in your example or is this feature really that bad?
    What is the point of named initialization if the order is still strict? If the order is strict, why can’t you mix named and unnamed initialization?
    To me this doesn’t make any sense.

  2. Out of order is not supported. See https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers: “The syntax forms (3,4) are known as designated initializers: each designator must name a direct non-static data member of T, and all designators used in the expression must appear in the same order as the data members of T.”

    My example is correct. It is ambiguous with all compilers that I tried (VC++, gcc, Clang). Here is the output from Clang:

    prog.cc:19:1: error: call to 'f' is ambiguous
    f({.a = 1, .b = 2});
    ^
    prog.cc:13:6: note: candidate function
    void f(A){}
    ^
    prog.cc:14:6: note: candidate function
    void f(B){}
    ^
    1 error generated.

  3. At this point the compilers can easily be wrong.

    Logically, if the order is strict than it can only be A, as for B the initialization list does not match.
    GCC and Clang only partially support designated initializers (VS support from 2019 16.1) yet, so i guess they will resolve that case, as the behavior you described is simply a contradiction.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.