Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units.
For example, if you have an instance of your class in the static storage in one module and you want to use it from the constructor of another class in the static storage in another module. In this case, you mean that the first instance will be initialized to the second. But the language does not have the ability to indicate this order if the instances are defined in separate modules.
Sometimes, when you create a library, it is necessary or just convenient to invent a type with a constructor and a destructor for the sole purpose of initializing and cleaning. This type will be used only once: select a static object so that the constructor and destructor are called.
This is useful when you are working with the 3rd part of libs, which requires an initialization and termination call. For example, WinSock 2 requires WSAStartup before you can call other WSA and WSACleanup functions when you are done with WinSock in your process. If you have a static instance of such a class that calls WSAStartup in the constructor and WSACleanup in the destructor, you should be able to use WSA functions in other places in your program (except for the constructors / destructors of other static objects).
source share