Initializing a struct with default values is trivial:
struct X { int a; int b = 2; };
and initializing a struct with a brace initializer is trivial too:
X x = {1, 3};
Suprisingly the init code won't compile, until I remove the default value. So, how would I do the init in such a case? I'd like to keep X a POD without c-tor.
When we define a struct (or class) type, we can provide a default initialization value for each member as part of the type definition. This process is called non-static member initialization, and the initialization value is called a default member initializer.
If a class has non-default constructors, the order in which class members appear in the brace initializer is the order in which the corresponding parameters appear in the constructor, not the order in which the members are declared (as with class_a in the previous example).
Using designated initializers, a C99 feature which allows you to name members to be initialized, structure members can be initialized in any order, and any (single) member of a union can be initialized. Designated initializers are described in detail in Designated initializers for aggregate types (C only).
Another method to initialize struct members is to declare a variable and then assign each member with its corresponding value separately. Note that char arrays can't be assigned with string, so they need to be copied explicitly with additional functions like memcpy or memmove (see manual).
If an aggregate type has a sub-aggregate (that is, another structure) element then the braces around the initializer of the sub-aggregate type may be omitted (elided). The compiler will take as many initializers from the list as required to initialize the element; the remaining iniitializers are then used to initialize any remaining members.
If you do, the braced initialization list becomes a call to a constructor. struct POD { int i; int j; POD (int x, int y); }; int main () { POD pod1 { 1, 2 }; // POD::POD (1, 2); }
For Modern C++ there is also a third difference: the way brace initialization is handled. For a structure, brace initialization performs a direct initialization of its members For class types, brace initialization results in a call to a constructor. There’s nothing to stop you adding a constructor to a struct.
Actually brace initialization is only allowed when there are no default member initializers in C++11. This limitation is lifted with C++14. ideone uses C++14 but if you use any C++11 compiler that code will fail. en.cppreference.com/w/cpp/language/aggregate_initialization What if the struct is defined using typedef, like C style? still initializes?
Here is some documentation relevant to the problem:
http://en.cppreference.com/w/cpp/language/aggregate_initialization
In c++11 your code is invalid. In c++14 it is valid again.
In C++11 adding a default initialization prevents braced init from being valid. In C++14, it does not.
A way to solve your problem in C++11 would be to write a constructor with the value for a and the b value with a default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With