fbpx

C++ Interview Question 13 Answer

 

Answer

This question is compilable and deterministic.

Its output is “abcBCA”.

Explanation

§3.6.2¶4 in the standard:
“It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized.”

Since A() is not constexpr, the initialization of a is dynamic. There are two possibilities:
a is initialized before main() is called, i.e. before b is initialized.
a is not initialized before main(). It is however guaranteed to be initialized before the the use of any function defined in the same translation unit, i.e. before the constructor of b is called.

When execution reaches B b, it is initialized as normal. Static local variables are initialized the first time control passes through their declaration, so c is initialized next. As main() is exited, its local variable bgoes out of scope, and is destroyed. Finally, all static variables are destroyed in reverse order of their initialization, first c, then a.

SOURCE