Are evaluations of expressions evaluated in the initialization of members relative to each other?

In particular, let's say I have:

struct X { X(int i) { cout << i; } };
int f() { cout << 'f'; return 0; }
int g() { cout << 'g'; return 1; }

struct Z {
    Z() : a(f()), b(g()) {}
    X a, b;
};

int main() { Z z; cout << '\n'; }

I know that element constructors are guaranteed to be called in the order in which they are defined in struct, so it 0will be printed before 1. But what about evaluating their arguments? Is it guaranteed:

f0g1

? Or maybe

fg01

and

gf01

are also valid outputs?

References to the standard are appreciated.

+4
source share
2 answers

++ 11 draft standard - , .

12.6.2 7 :

[...] , mem-, . mem- , . [...]

1.9 14 :

, , , , .

12.6.2 :

ctor-initializer:
   : mem-initializer-list
mem-initializer-list:
   mem-initializer ...opt
   mem-initializer , mem-initializer-list ...opt
[...]

Pre ++ 11 mem-, , , , 1804. , undefined pre ++ 11 , pre ++ 11.

+5

++

, mem-, . mem- ,

, a (f()), b (g()).

f0g1
+2

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


All Articles