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,
T object = {arg1, arg2, ...};
|
In C++11 and above, we use without = as below,
T object {arg1, arg2, ...};
|
In C++20, there are 3 new options that we can use,
T object (arg1, arg2, ...); T object = { .designator = arg1 , .designator { arg2 } ... }; T object { .designator = arg1 , .designator { arg2 } ... };
|
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,
struct st_xy { float x, y; };
struct st_xy xy{ 3.2f, 5.1f };
|
In modern C++, consider that we have a struct that has a, b, c members. We initialize first two members as below,
struct st_x { short int a, b, c; };
struct st_x x{ .a = 10, .b = 20}; // x.c will be 0
|
We can directly initialize as below too,
struct st_y { int a = 100, b = 200, c, d; } y;
|
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,
struct st_z : st_y { int e; };
struct st_z z{ 1, 2, 3, 4, 5};
|
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,
- private or protected non-static data members,
- a constructor that is user-provided, inherited, or explicit constructors (explicitly defaulted or deleted constructors are allowed),
- base class or classes (virtual, private, protected),
- virtual member functions
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,
- private or protected non-static data members,
- user-declared or inherited constructors,
- base class or classes (virtual, private, protected),
- virtual member functions
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include
// Aggregate in C++14 struct st_xy { float a, b; };
struct st_xy xy{ 3.2f, 5.1f };
struct st_x { short int a, b, c; };
struct st_x x{ .a = 10, .b = 20}; // x.c will be 0
struct st_y { int a = 100, b = 200, c, d; } y;
// Aggregate in C++17 struct st_z : st_y { int e; };
struct st_z z{ 1, 2, 3, 4, 5};
int main() { std::cout << “xy:” << xy.a << “,” << xy.b << std::endl;
std::cout << “x:” << x.a << “,” << x.b << “,” << x.c << std::endl;
std::cout << “y:” << y.a << “,” << y.b << “,” << y.c << std::endl;
std::cout << “z:” << z.a << “,” << z.b << “,” << z.c << “,” << z.d << “,” << z.e << std::endl;
system(“pause”); return 0; }
|
and the output will be as below,
xy:3.2,5.1 x:10,20,0 y:100,200,0 z:1,2,3,4,5 Press any key to continue . . .
|
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