C++ Static Initialization Order Fiasco Workaround

The C++ Static Initialization Order Fiasco (SIOF) can happen when 2 classes have static initializers and one classes static initializer calls the other class before its static initializer has been executed. The order of static initializer execution is determined by the linker.

SIOF Example

A parser that stores some regexes.

Parser.h

class Parser {
static std::map<std::string, Regex> regexs;
}

Parser.cpp

using namespace std;
map<string, Regex> Parser::regexs = {
{"table" , Regex("!\\{\\|[^{]*?\\|\\}!m")},
{"link" , Regex("!\\[\\[[^\\[\\]]*?\\]\\]!")}
};

Regex.h

class Regex {
static std::map<char, int> modifiers;
Regex(string& regex);
}

Regex.cpp

using namespace std;
map<char, int> Regex::modifiers {
{'i', PCRE_CASELESS},
{'m', PCRE_MULTILINE}
};

Regex::Regex(string& regex) {
modifiers['m'] could fail
}

Parser::regexes and Regex::modifers are both globally initialized statics.

Depending on the linker, Parser::regexes could get initialized before Regex::modifers. If that happens, then the reference to modifiers['m'] will fail in the Regex constructor that the Parser::regexes initializer calls.

C++11 Workaround

Create a static unique_ptr to a map in a helper method.

Regex.h

class Regex {
std::unique_ptr<std::map<char, int>>& getModTable();
Regex(string& regex);
}

Regex.cpp

using namespace std;
unique_ptr<map<char, int>>& Regex::getModTable()
{
static unique_ptr<map<char, int>> modifiers(new map<char, int>);

if (! modifiers->empty()) return modifiers;

modifiers->insert(pair<char,int>('i', PCRE_CASELESS));
modifiers->insert(pair<char,int>('m', PCRE_MULTILINE));

return modifiers;
}

Regex::Regex(string& regex) {
unique_ptr<map<char, int>>& modifiers = getModTable();
modifiers['m'] will not fail
}

Before the getModTable() method is executed for the first time, the locally initialized static unique_ptr is initialized to a new empty map. If the map is empty, then it is filled.