# C++ Static Initializers and the Static Initialization Order Fiasco

DevFeed: [C++ Static Initializers and the Static Initialization Order Fiasco](<https://devfeed.tech/articles/static-initializers-will-murder-your-family-35545.md>)

Original publisher: [Read original article](<https://meowni.ca/posts/static-initializers/>)

Author: Monica Dinculescu

Published: 2014-04-22T00:00:00Z

Content type: tutorial

Language: en

Sources: [Monica Dinculescu](<https://devfeed.tech/sources/monica-dinculescu.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [variables](<https://devfeed.tech/tags/variables.md>)

## AI overview

This article explains C++ static initializers, including how runtime initialization differs from constant initialization and how initialization order can become undefined across translation units. It discusses the resulting static initialization order fiasco.

## Source excerpt

But only if your family is code. So this is a bit of a terrible blog post because a) it's about a really obscure atrocity that happens in C++ (as opposed to the common atrocities that happen in C++ on the regs) and b) there are not enough funnies in the world to make up for it. I recommend skipping it if you've just eaten, are feeling light-headed, or don't want to make eye contact with C++. As a general policy, you should probably never make eye contact with C++. It can smell fear. Programmer, meet static initializers We're going to be talking about static class objects, or objects defined in a global/unnamed namespace, such as these fellas: namespace { static const std::string kSquirrel = "sad squirrel"; static const Superhero batman; } // or class Foo { static const std::string panda_ = "also a sad panda"; } Static initialization is the dance we do when creating these objects. This is not a dance we do when we initialize things with constant data (like static int x = 42); the compiler sees that the thing after the = is constant and can't change, so it can inline it. However, if you try to initialize a variable by running code (e.g. static int x = foo()), then this is not a constant anymore, and it will result in a static initializer. In C++11, I think constexpr will let you hint to the compiler that the thing after the equal is a constant expression, if it is that, so it can compute it at compile-time. I don't get to use a lot of C++11, so this is still about nightmares of C++ past, and I don't think constexpr will do away with all of the murders anyway. Finally, the compiler promises you to run all the static initializers before the body of main() is executed. That, unfortunately, doesn't mean much. Why static initializers are bad news bears As Douglas Adams, the inventor of C++ said, static initializers have "made a lot of people very angry and been widely regarded as a bad move". Apart from being hard to spell, they tend to throw up on your shoes: Static varia