I have C++ code which declares static-lifetime variables which are initialized by function calls. The called function constructs a vector instance and calls its push_back method. Is the code risking doom via the C++ static initialization order fiasco? If not, why not?
Supplementary information:
What's the "static initialization order fiasco"?
It's explained in C++ FAQ 10.14
Why would I think use of vector could trigger the fiasco?
It's possible that the
vectorconstructor makes use of the value of another static-lifetime variable initialized dynamically. If so, then there is nothing to ensure thatvector's variable is initialized before I usevectorin my code. Initializingresult(see code below) could end up calling thevectorconstructor beforevector's dependencies are fully initialized, leading to access to uninitialized memory.What does this code look like anyway?
struct QueryEngine { QueryEngine(const char *query, string *result_ptr) : query(query), result_ptr(result_ptr) { } static void AddQuery(const char *query, string *result_ptr) { if (pending == NULL) pending = new vector<QueryEngine>; pending->push_back(QueryEngine(query, result_ptr)); } const char *query; string *result_ptr; static vector<QueryEngine> *pending; }; vector<QueryEngine> *QueryEngine::pending = NULL; void Register(const char *query, string *result_ptr) { QueryEngine::AddQuery(query, result_ptr); } string result = Register("query", &result);