This example has been lifted from the Q & A section here:http://stackoverflow.com/a/1008289/3807729
See this article for a simple design for a lazy evaluated with guaranteed destruction singleton:
Can any one provide me a sample of Singleton in c++?
The classic lazy evaluated and correctly de...
C++11
The C++11 standards guarantees that the initialization of function scope objects are initialized in a synchronized manner. This can be used to implement a thread-safe singleton with lazy initialization.
class Foo
{
public:
static Foo& instance()
{
static Foo inst;
...
There are times with multiple static objects where you need to be able to guarantee that the singleton will not be destroyed until all the static objects that use the singleton no longer need it.
In this case std::shared_ptr can be used to keep the singleton alive for all users even when the static...