The C++20 standard provides new ways to initialize aggregates. These are:
- list initialization with designated initializers, that has the following forms:
T object = { .designator = arg1 , .designator { arg2 } ... }; T object { .designator = arg1 , .designator { arg2 } ... };
- direct initialization, that has the following form:
T object (arg1, arg2, ...);
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:
struct foo { int a; char c = 'a'; } foo f { .a = 42 };
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:
struct bar { int x; }; struct foo { int a; bar b; char c = 'a'; double d; };
The following inialization is allowed:
foo f1{}; // OK: a = 0, b = {x = 0}, c = 'a', d = 0.0 foo f2{ .a = 42 }; // OK: a = 42, b = {x = 0}, c = 'a', d = 0.0 foo f3{ .a = 42, .c = 'b' }; // OK: a = 42, b = {x = 0}, c = 'b', d = 0.0 foo f4{ .a = 42, .b = {.x = 5} }; // OK: a = 42, b = {x = 5}, c = 'a', d = 0.0 foo f5{ .a = 42, .b = {5} }; // OK: a = 42, b = {x = 5}, c = 'a', d = 0.0
However, the following forms of initialization are illegal:
foo f6{ .d = 1, .a = 42 }; // ERROR: out-of-order foo f7{ .a = 42, true, 'b', 1 }; // ERROR: mixed designated and non-designated foo f8{ .a = 42, .a = 0 }; // ERROR: duplicate designator foo f9{ .b.x = 42 }; // ERROR: nested initializer int arr[5] = { [0] = 42 }; // ERROR: array designators not allowed
Here are several more examples. Consider the following classes and functions:
struct A { int a, b; }; struct B { int b, a; }; struct C { int a, c; }; void f(A){} void f(B){} void f(C){} void g(B){}
The following calls are permitted:
f({ .a = 1, .c = 2 }); // OK: calls f(C) g({ .b = 1, .a = 2 }); // OK: calls g(B)
However, the following calls are, on the other hand, erroneous:
f({.a = 1, .b = 2}); // ERROR: ambiguous between f(A) and f(B) f({.a = 1}); // ERROR: ambiguous call, f(A), f(B), or f(C) g({.a = 1, .b = 2}); // ERROR: g(B) but designators are in the wrong order
A designated initializer, and only one, can be used to initialize a union. Let us consider the following union type:
union Foo { int a; bool b; char c; double d; };
The following forms of initialization are correct:
Foo u1{}; // OK: a = 0 Foo u2{ .a = 1 }; // OK: a = 1 Foo u3{ .c = 'b' }; // OK: c = 'b'
However, having more than one designator is not allowed:
Foo u4{ .a = 1, .b = true }; // ERROR: cannot have more than one element
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.
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.
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.
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.
I agree with what you are saying. It should not be a contradiction because B can be fully resolved from the order of the designators and A cannot.
Marius that’s a great blog post. Curious to know what was the motivation to provide the designated initializer feature ? was it to achieve parity with C ?