Here in cppref says,
If the initialization of a non-inline variable (since C++17) is deferred to happen after the first statement of main/thread function, it happens before the first odr-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized.
And later it gives an example of deferred dynamic initialization:
// - File 1 -
#include "a.h"
#include "b.h"
B b;
A::A(){ b.Use(); }
// - File 2 -
#include "a.h"
A a;
// - File 3 -
#include "a.h"
#include "b.h"
extern A a;
extern B b;
int main() {
a.Use();
b.Use();
}
And the comment says:
If
a
is initialized at some point after the first statement ofmain
(which odr-uses a function defined inFile 1
, forcing its dynamic initialization to run), thenb
will be initialized prior to its use in A::A
Why can the if situation happen? Doesn't a.Use()
odr-use a
thus a
must be initialized before this statement?