Site icon Learn C++

What Is Aggregate Member Initialization In C++?

What Is Aggregate Member Initialization In C++

The Aggregate Member Initialization is one of the features of C++. This feature is improved and modernized with C++11, C++14 and C++20. With this feature, objects can initialize an aggregate member from braced-init-list. In this post, we explain what the aggregate member initialization is and what were the changes to it in modern C++ standards.

What is aggregate member initialization in modern C++?

Aggregate initialization initializes aggregates. Since C++11, aggregates are a form of listed initializations. Since C++20 they are direct initializations. An aggregate could be an array or class type (a class, a struct, or a union). 

Here is the general syntax,

[crayon-663ef6a747a84805028318/]

In C++11 and above, we use without = as below,

[crayon-663ef6a747a8a520978184/]

In C++20, there are 3 new options that we can use,

[crayon-663ef6a747a8c965106088/]

How to use aggregate member initialization in modern C++?

C++14 provides a solution to problems in C++11 and above, for example in C++14, consider we have x, y coordinates in a struct, we can initialize them as below in a new xy object,

[crayon-663ef6a747a8e635222143/]

In modern C++, consider that we have a struct that has a, b, c members. We initialize first two members as below,

[crayon-663ef6a747a90399417360/]

We can directly initialize as below too,

[crayon-663ef6a747a92676770791/]

In C++17 and above, we can use this st_y as a base and we can add a new member to a new struct, then we can initialize as below,

[crayon-663ef6a747a94653643213/]

What restrictions are there for the aggregate member initialization in C++?

If we consider the C++17 standard, an aggregate initialization can NOT be applied to a class type if it has one of the below,

If we consider the C++20 standard, an aggregate initialization can NOT be applied to a class type if it has one of the below,

Is there a full example of aggregate member initialization in C++?

Here is a full example that explains simply most used features of aggregate member initialization,

[crayon-663ef6a747a97486947932/]

and the output will be as below,

[crayon-663ef6a747a99654552515/]

For more information about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3651.pdf

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here



Class type shuld

Exit mobile version