Prevent static initialization order "fiasco", C++

Solution 1:

The modern, more pattern-oriented way is not to use globals in the first place.

There's no other way around it.

It wouldn't be much of a "fiasco", otherwise!

Solution 2:

So my question is, is there any modern, more pattern oriented way to prevent this "fiasco" but to wrap the "static stuff" into functions???

In most cases, you can declare your "global" data in the main function, and use dependency injection to pass it around, where needed. In other words, do not have static state at all.

In practice, you can have situations where static data is needed. If there are no dependencies to other statics, make the static data const/constexpr.

// smart pointer that implements the "Foo" release policy
class FooPointer
{
    static const FooPointer NullFoo; // does not depend on other static values
    /* ... */
};

In case the static variables do depend on each other, just wrap them in static functions:

// smart pointer that implements the "Foo" release policy
class FooPointer
{
    static const FooPointer& NullFoo(); // depends on other static values
    /* ... */
};

To summarize:

Most (90%? 99%?) static/global/shared data should be dependency-injected into where it is used, and not created as static at all.

In the rare cases when statics are required for a reason or another and they do not depend on other statics, declare static variables.

In the very rare cases when statics need to be static and they depend on each other, wap them in static methods.

As a rule of thumb, if you have a lot of the second and third cases, you are not doing enough of the first.