C++14 brought us a lot of useful things that we use today. One of the great features of C++14 was the member initializers and aggregates feature that a class with default member initializers may now be an aggregate. In this post, we explain how can use member initializers and aggregates with examples.
What are the member initializers and aggregates features in C++14?
With the new C++14, a class or struct with default member initializers may now be an aggregate in definition.
“If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list“. Here is an example,
1 2 3 4 |
struct st_X { int a; int b = 100; }; st_X X = { 5 }; |
Here, X.b will be 100 automatically.
Are there simple examples about member initializers and aggregates features in C++14?
This feature can be good to be used with default values (i.e. “Unknown”), here is an example.
1 2 3 4 5 6 7 8 9 10 11 12 |
struct st_student { std::string name; int score; std::string uni = "Unknown"; }; st_student s0 = { "David Millington"}; st_student s1 = { "Yilmaz Yoru", 98 }; st_student s2 = { "Ian Barker", 99, "Harvard University" }; |
Here, David will have 0 score and “Unknown” university, Yilmaz will have “Unknown” university.
We can use parameters in the next parameters.
1 2 3 4 |
struct st_test { const char* str; int a; int val = str[a]; }; st_test test = { "ABCD", 2 }; |
Here example above, value of test.val is initiated automatically to 67 which is third char ‘C’.
Is there a full example about member initializers and aggregates feature in C++14?
Here is a full example.
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 |
#include <iostream> #include <string> struct st_student { std::string name; int score; std::string uni = "Unknown"; }; struct st_test { const char* str; int a; int val = str[a]; }; struct st_X { int a; int b = 100; }; int main() { st_student s0 = { "David Millington"}; st_student s1 = { "Yilmaz Yoru", 98 }; st_student s2 = { "Ian Barker", 99, "Harvard University" }; std::cout << s0.name << "," << s0.score << "," << s0.uni << std::endl; std::cout << s1.name << "," << s1.score << "," << s1.uni << std::endl; std::cout << s2.name << "," << s2.score << "," << s2.uni << std::endl << std::endl; st_test test = { "ABCD", 2 }; std::cout << test.str << "," << test.a << "," << (char)test.val << std::endl << std::endl; st_X X = { 5 }; std::cout << X.a << "," << X.b << std::endl; system("pause"); return 0; } |
The output of this example will be as follows:
1 2 3 4 5 6 7 8 9 |
David Millington,0,Unknown Yilmaz Yoru,98,Unknown Ian Barker,99,Harvard University ABCD,2,C 5,100 |
For more details, please see,
this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3605.html ,
and this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3653.html
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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.