Category Archives: C++

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.
Continue reading C++ Static Initialization Order Fiasco Workaround