2

c.hpp:

class C
{
private:
    static SomeClass var;

public:
    static void f()
    {
        // Uses var;
    }
};

c.cpp:

SomeClass C::var;

Is it always safe to call C::f()? For instance, from constructor of some global variable defined in a different compilation unit?

user2052436
  • 4,321
  • 1
  • 25
  • 46

1 Answers1

4

No. The initialization order of all but function-local static variables leads to disaster in the worst way possible.

The technical term is "Static Initialization Order Fiasco". It's real, and googleable.

The trick is to not use globals in any form. Function-local static are incredibly useful and should be used when appropriate if you know when they are so incredibly useful.

rubenvb
  • 74,642
  • 33
  • 187
  • 332