Consider:
int f () {
static int i = 0;
return i++;
}
struct Test {
int a, b;
Test () : a(f()), b(f()) {}
};
Test t;
I know that a is initialized before b due to the order of their declaration in the struct.
I also know that the two calls to f in g(f(), f()) are unsequenced.
So I am wondering if it is guaranteed that t.a == 0 and t.b == 1?
This will always be true so long as
acomes beforebin the class declaration and nothing else callsf()between the initialization ofaandb. Class members are initialized in the order they are declared in the class. [class.base.init]/11:So since
acomes beforebthen when the constructor initializesait will callf()the first time and then it will call it a second time when it initializesb.We also know there is a sequence point between member initializer because [class.base.init]/7:
tells us each initializer is a full expression and each full expression is sequenced: [intro.execution]/14