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.

3,534 thoughts on “C++ Static Initialization Order Fiasco Workaround”

  1. Pingback: React Element 87
  2. Good ?V I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your customer to communicate. Excellent task..

  3. You actually make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  4. Simply want to say your article is as astonishing. The clearness on your submit is just spectacular and that i could assume you’re knowledgeable on this subject. Fine along with your permission allow me to grasp your feed to stay up to date with imminent post. Thanks one million and please keep up the gratifying work.

  5. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  6. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  7. Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam feedback? If so how do you prevent it, any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any assistance is very much appreciated.

  8. The heart of your writing while appearing reasonable at first, did not sit properly with me after some time. Somewhere throughout the paragraphs you actually managed to make me a believer but just for a while. I nevertheless have got a problem with your jumps in logic and you might do nicely to help fill in those gaps. In the event you can accomplish that, I will definitely end up being impressed.

  9. I¦ve been exploring for a bit for any high-quality articles or blog posts in this kind of house . Exploring in Yahoo I ultimately stumbled upon this website. Reading this information So i am glad to express that I have a very excellent uncanny feeling I found out exactly what I needed. I most unquestionably will make certain to do not disregard this web site and provides it a glance on a continuing basis.

  10. Pingback: Yeezys
  11. What i do not understood is in reality how you are now not really a lot more well-appreciated than you may be right now. You’re very intelligent. You understand therefore significantly when it comes to this subject, produced me in my opinion consider it from a lot of numerous angles. Its like men and women are not involved except it is something to accomplish with Woman gaga! Your individual stuffs nice. Always handle it up!

  12. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  13. My brother suggested I might like this website. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  14. I do not even understand how I finished up right here, however I believed this publish was once great. I do not recognise who you might be however certainly you’re going to a well-known blogger in the event you are not already 😉 Cheers!

  15. I’m impressed, I have to say. Actually not often do I encounter a blog that’s both educative and entertaining, and let me let you know, you have got hit the nail on the head. Your concept is outstanding; the issue is something that not sufficient people are speaking intelligently about. I am very completely happy that I stumbled throughout this in my search for something referring to this.

  16. I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

  17. Good – I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your customer to communicate. Excellent task..

  18. Can I just say what a aid to search out someone who really knows what theyre speaking about on the internet. You positively know how one can deliver a difficulty to mild and make it important. More individuals have to read this and understand this side of the story. I cant imagine youre not more popular because you definitely have the gift.

  19. Pingback: Nike Air Max 2019
  20. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say fantastic blog!

  21. Pingback: Salomon
  22. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  23. What i don’t understood is if truth be told how you are not actually much more smartly-favored than you might be right now. You’re so intelligent. You understand therefore considerably in relation to this matter, produced me in my opinion imagine it from so many various angles. Its like women and men aren’t interested until it is one thing to accomplish with Woman gaga! Your individual stuffs nice. All the time care for it up!

  24. Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appeal. I must say you have done a amazing job with this. In addition, the blog loads super quick for me on Chrome. Excellent Blog!

  25. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read?

  26. Pingback: Pandora.com
  27. Hey very nice website!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…I am happy to find a lot of useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

  28. Pingback: Yeezy
  29. Great post. I used to be checking constantly this weblog and I’m inspired! Very helpful information particularly the remaining part 🙂 I handle such info a lot. I used to be looking for this certain information for a long time. Thanks and best of luck.

  30. Hi, Neat post. There’s a problem with your site in internet explorer, would check this… IE still is the market leader and a huge portion of people will miss your wonderful writing because of this problem.

  31. After examine a couple of of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark web site checklist and might be checking back soon. Pls take a look at my site as effectively and let me know what you think.

  32. Pingback: Yeezy Shoes
  33. I precisely desired to appreciate you once more. I am not sure the things that I would’ve achieved without the type of opinions provided by you about such area of interest. This was the horrifying concern in my circumstances, nevertheless finding out the very professional tactic you solved that made me to cry with delight. Now i’m happy for this assistance as well as hope you realize what a great job that you’re getting into training the others via your website. I am sure you have never met any of us.

  34. Hello! I could have sworn I’ve been to this website before but after going through a few of the posts I realized it’s new to me. Anyways, I’m certainly happy I came across it and I’ll be book-marking it and checking back regularly!

  35. What i do not realize is if truth be told how you are now not really much more neatly-preferred than you may be right now. You are very intelligent. You realize therefore significantly on the subject of this topic, produced me individually believe it from numerous various angles. Its like men and women aren’t fascinated unless it is something to do with Woman gaga! Your individual stuffs nice. All the time handle it up!

  36. Pingback: Nike Outlet
  37. This design is spectacular! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  38. We’re a group of volunteers and starting a new scheme in our community. Your website provided us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to you.

  39. Great work! That is the type of information that are supposed to be shared around the net. Shame on Google for now not positioning this submit higher! Come on over and talk over with my website .

  40. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase.

  41. Very nice post. I just stumbled upon your blog and wanted to say that I ave really enjoyed browsing your blog posts. After all I all be subscribing to your rss feed and I hope you write again soon!

  42. I wish to show my love for your kindness supporting men and women that require guidance on in this area. Your real commitment to passing the solution throughout was rather advantageous and has continuously allowed individuals like me to arrive at their targets. Your amazing warm and friendly help and advice denotes this much to me and somewhat more to my peers. With thanks; from each one of us.

  43. Awesome website you have here but I was wanting to know if you knew of any user discussion forums that cover the same topics talked about here? I’d really like to be a part of online community where I can get opinions from other experienced people that share the same interest. If you have any suggestions, please let me know. Kudos!

  44. Pingback: Balenciaga UK
  45. Well said. I never thought I would agree with this opinion, but I’m starting to view things from a different view. I have to research more on this as it seems very interesting. One thing I don’t understand though is how everything is related together.

  46. What’s Going down i am new to this, I stumbled upon this I’ve discovered It positively helpful and it has aided me out loads. I’m hoping to give a contribution & help other users like its helped me. Great job.

  47. Today, I went to the beachfront with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

  48. I like what you guys are up too. Such smart work and reporting! Carry on the excellent works guys I?¦ve incorporated you guys to my blogroll. I think it’ll improve the value of my web site 🙂

  49. Hey there! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Kudos!