Open semushin-s opened 1 week ago
Another interesting aspect of this issue, coming from how it was discovered originally. Initialization order may be affected by adding irrelevant global constants to original example. Basically it comes to the following:
const std::string
global constant on top makes initialization work expectedly in clang trunk but only with C++17. Link to godboltMight not be that relevant since it might be just optimization noise, but makes it more surprising when encountering this in the wild.
Please take a look at the following example:
Currently it returns 0 with
-O2
using clang: https://gcc.godbolt.org/z/66hGqoaj8 and returns 1 with-O0
. Compiled by all the other compilers, it results in 1 as well.It seems initialization of
static inline A _ca
should be done earlier than initialization ofB
. According to[basic.start.dynamic]
:if V has partially-ordered initialization, W does not have unordered initialization, and for every definition E of W there exists a definition D of V such that D is appearance-ordered before E ... V is sequenced before the initialization of W;
Let's say
static inline A _ca
is definition ofV
,B b
is definition ofW
, seems by appearance order_ca
should have been initialized beforeb
, however with optimizationa=1
initialization fails to happen before initializingB b
apparently. Please correct me if my interpretation of the standard is wrong in this case.