memory: Construct globals on first use #15266
The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed.
Specifically this fixes:
promag left a comment •
An alternative is to use templating and also defer instantiation to
```cpp #include #include
using namespace std;
string* const g_str_old = new string();
cout << *g_str_old << endl;
@MarcoFalke Not sure if that's what you're referring to, but the COFU object has no member variables to initialize, so it doesn't have any runtime initialization (in other words, it's fully constructed before any code is executed).
I don't have a strong opinion on whether such a COFU object/type is the right solution; just offering it as a possibility if reducing the size of the diff is wanted.
Relevant section from https://en.cppreference.com/w/cpp/language/constant_initialization:
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.