cpp/language/aggregate initialization

Initializes an aggregate from an initializer list.

Syntax
@1,2@ Initializing an aggregate with an ordinary initializer list. @3,4@ Initializing an aggregate with designated initializers (aggregate class only).

Definitions
An aggregate is one of the following types:
 * array type
 * class type (typically, or ), that has


 * no private or protected non-static data members


 * no virtual member functions

The elements of an aggregate are:
 * for an array, the array elements in increasing subscript order, or

Process
The effects of aggregate initialization are:

@1@ Reject the following ill-formed cases:
 * the number of initializer clauses in the initializer list exceeds the number of elements of the aggregate, or
 * initialize an array of unknown bound with an empty initializer list.

@2@ Determine the explicitly initialized elements of the aggregate as follows:


 * Otherwise, if the initializer list is non-empty, the explicitly initialized elements of the aggregate are the first n elements of the aggregate, where n is the number of elements in the initializer list.
 * Otherwise, the initializer list must be empty, and there are no explicitly initialized elements.


 * The program is ill-formed if the aggregate is a union and there are two or more explicitly initialized elements:

@3@ Initialize each element of the aggregate.

Initializing Elements
For each explicitly initialized element:


 * If the initializer clause is an expression, are allowed as per copy-initialization.
 * If the initializer clause is a nested (which is not an expression),  recursively apply the rule if the corresponding element is a subaggregate.

For a non-union aggregate, each element that is not an explicitly initialized element is initialized as follows:


 * Otherwise, if the element is not a reference, the element is copy-initialized from an empty initializer list.
 * Otherwise, the program is ill-formed.

If the aggregate is a union and the initializer list is empty, then


 * Otherwise, the first member of the union (if any) is copy-initialized from an empty initializer list.

Brace elision
The braces around the nested initializer lists may be elided (omitted), in which case as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object. However, if the object has a sub-aggregate without any members (an empty struct, or a struct holding only static members), brace elision is not allowed, and an empty nested list must be used.

{{rrev|since=c++20|

Designated initializers
The syntax forms are known as designated initializers: each  must name a direct non-static data member of T, and all s used in the expression must appear in the same order as the data members of T.

Each direct non-static data member named by the designated initializer is initialized from the corresponding brace-or-equals initializer that follows the designator. Narrowing conversions are prohibited.

Designated initializer can be used to initialize a into the state other than the first. Only one initializer may be provided for a union.

For a non-union aggregate, elements for which a designated initializer is not provided are initialized the same as described above for when the number of initializer clauses is less than the number of members (default member initializers where provided, empty list-initialization otherwise):

If the aggregate that is initialized with a designated initializer clause has an anonymous union member, the corresponding designated initializer must name one of the members of that anonymous union.

Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++.

}}

Character arrays
Arrays of ordinary character types, or can be initialized from ordinary s, or wide string literals, respectively, optionally enclosed in braces. Successive characters of the string literal (which includes the implicit terminating null character) initialize the elements of the array. If the size of the array is specified and it is larger than the number of characters in the string literal, the remaining characters are zero-initialized.