template<typename T>
class anch::Singleton< T >
Meyers' singleton implementation.
The instance is created on first getInstance call. Its creation is thread safe according to the standard.
If T constructor throws an exception, the next getInstance call will try to create the instance again.
Usage:
friend anch::Singleton<MySingleton>;
private:
MySingleton();
~MySingleton();
};
T has to inherit from anch::Singleton<T> ;
T constructor and destructor should be private and anch::Singleton<T> declared as friend to prevent other instances ;
- copy and move are prohibited by this class: there is no need to delete them in
T ;
T destructor does not need to be virtual: the instance is never destroyed through a base class pointer.
Destruction order: singletons are destroyed at program exit, in the reverse order of the end of their construction. When a singleton uses another one in its destructor (or holds objects which depend on it), it has to call the other singleton's getInstance in its constructor: the other singleton will be fully constructed first, so it will be destroyed after.
Singleton has default symbols visibility: the instance is shared between AnCH libraries (built with hidden visibility) and the application.
A singleton must not be used from a static object destructor or a detached thread after its destruction.
- Since
- 0.1
- Author
- Vincent Lachenal