[c++] Aggregate initialization improvements

C++20 introduces several improvements to aggregate initialization, making it more flexible and powerful. In this blog post, we will explore these enhancements and see how they can benefit C++ developers.

Table of Contents

Enhanced Aggregate Initialization

In C++11, aggregate initialization allowed initialization of aggregates using brace-enclosed initializers. However, it had limitations such as disallowing parentheses and requiring the initializer list to be in the same order as the data members.

C++20 relaxes these restrictions and widens the scope of aggregate initialization. It now allows the use of parentheses for member initialization and does not mandate the same order of initializer list as the members.

With this improvement, C++20 facilitates cleaner and more concise aggregate initializations, providing a more natural and flexible syntax.

Here’s an example of the new aggregate initialization syntax in C++20:

// C++11
struct Point {
    int x;
    int y;
};

Point p1 = {1, 2};  // brace-enclosed initializer

// C++20
struct Point {
    int x;
    int y;
};

Point p2 = { .y = 2, .x = 1 };  // new aggregate initialization with designated initializers

Designated Initializers

Another significant enhancement in C++20 is the introduction of designated initializers, a feature inspired by C. This feature allows the initialization of specific members of an aggregate using designated initializers.

Designated initializers provide more control and clarity in aggregate initialization by allowing developers to specify which members they want to initialize, thereby avoiding the need to rely on the order of the members.

A designated initializer is composed of a member name followed by an equals sign and the value to be assigned. This syntax enables selective and explicit member initialization.

// C++20 designated initializers
Point p3 = { .x = 3, .y = 4 };  // initializing specific members with designated initializers

The introduction of designated initializers in C++20 brings more expressive power to aggregate initialization, enhancing readability and ease of use.

References