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 { […]
