Consider the following complete program consisting of two TU
's:
// 1.cpp
bool init() { /* ... */ }
const auto _{init()};
// 2.cpp
int main() {}
Question: is there any guarantee that _ is initialized at some point (I do not care when)?
Now consider the program consisting of one TU
:
// 1.cpp
bool init() { /* ... */ }
const auto _{init()};
int main() {}
Note that _ is not odr-used.
However, can main()
, in the second case, be said to be odr-used
, since it gets (sort of) "referred by the implementation" as it gets called when the program is run?
And if main()
is odr-used
, does this imply that _ is guaranteed to be initialized even if it's not odr-used
?
EDIT:
This is what en.cppreference.com says about Deferred dynamic initialization:
If no variable or function is odr-used from a given translation unit, the non-local variables defined in that translation unit may never be initialized (this models the behavior of an on-demand dynamic library)
Can you answer my questions considering the above when reading my two examples?