Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brace (aggregate) initialization for structs with default values

Tags:

c++

c++11

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.

like image 613
Mike Lischke Avatar asked Sep 06 '16 08:09

Mike Lischke


People also ask

Can C++ structs have default values?

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.

What is brace initialization?

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).

Can struct have Initializers?

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).

How do you initialize a struct value?

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).

Why are the braces around the initializers of an aggregate type omitted?

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.

What happens if you brace initialization list in constructor?

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); }

What is the difference between brace initialization and class initialization in C++?

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.

Is brace initialization allowed in C++14?

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?


Video Answer


1 Answers

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.

like image 80
Hayt Avatar answered Sep 20 '22 16:09

Hayt