The C++17 standard contains an example almost exactly the same as in the question:
struct B1 { B1(int); /* ... */ };
struct B2 { B2(int); /* ... */ };
struct D : B1, B2 {
D(int);
B1 b;
const int c;
};
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { /* ... */ }
D d(10);
This is followed by a note:
[ Note: The initialization performed by each mem-initializer constitutes a full-expression (4.6). Any expression in a mem-initializer is evaluated as part of the full-expression that performs the initialization. — end note ]
Following the link, section 4.6 tells us that one of the definitions of "full-expression" is
... a mem-initializer, including the constituent expressions of the initializer,
The phrase "including constituent expressions of the initiailizer" strongly suggests to me that the above code is legal, because the side effects of ++i
will have completed before moving on to the next initializer. This is just my reading of the standard though, I'm happy to defer to anyone with more standardese experience than me.
(It's also worth noting that initialization of members will happen in the order in which they are declared in the class, not in the order in which they appear in the member initializer list).
a
,b
andx
if you are doingWrinkle tmp{1}
? – JenninejenningsWrinkle(int i) : a(i+1), b(i+2), x(i+3) {}
– Moyea, x, b
. – Hedrick