I'm not sure that you already know this, but you need to remember that Singleton is indeed a global variable with lazy initialization .
Lazy initialization is a tool to fix the problem of initializing an object always when you really want to use it - whether for some function of a real program or to initialize another dependent object. This is done to delay initialization until the first time the object is used.
A static object is simply initialized at the moment when it needs to create the first one , however, when it really is, it is undefined, at least in C ++.
You can replace lazy initialization with static initialization, but you must somehow make sure that the initialization happens in a specific order.
Defining variables within a namespace is nothing more than declaring variables globally. Namespaces are open, the rules inside the namespace are the same as outside the namespace, except for the resolution of the character.
What you can do to force initialization is to create one global variable with all the dependent global objects inside, in the form of a struct that will contain all of them as fields ( not static fields!). Please note that the exact initialization order will be provided only between objects that are fields of this structure, and not between them and any other global objects.
source share