C ++ evaluation order between initializer with equal or equal value and initialization list?

If I have this structure,

struct AAA { AAA() : bbb(2) { // ccc ??? } int bbb = 1; int ccc = bbb; }; 

AFAIK , if there is an initialization list :bbb(2) , the expression bbb = 1 will be ignored. And then, it is uncertain for me that ccc will become final.

Which of the initializers-list-initializers or parentheses-peers will be evaluated first? What is the rule between them?

+6
source share
2 answers

The rule has always been that fields are always initialized in the order of declaration, and C ++ 11 does not change this. This means that the bbb initializer starts first, then the ccc initializer starts. It does not matter if the initializer is specified in the field or as part of the constructor.

+11
source

C ++ 11 draft ยง12.6.2.9 says:

If a given non-static data member has both a sliding or peer-initializer and a mem-initializer, the initialization specified by the mem-initializer and the non-static data element associated with the curly bracket or peer-initializer are ignored.

[Example: given

 struct A { int i = /โˆ— some integer expression with side effects โˆ—/ ; A(int arg) : i(arg) { } // ... }; 

the constructor A (int) simply initializes I with the arg value, and side effects in the bond or alignment mode will not be accepted. - end of example]

Since initialization is performed in the declaration order (ยง12.6.2.10) with the addition of this rule, the value of bbb and ccc will be 2.

+12
source

Source: https://habr.com/ru/post/959088/


All Articles