Template Class Singleton

Class Documentation

template<typename T>
class Singleton

Singleton construction via the curiously recurring template pattern.

This class doesn’t provide singletons, but enables the construction of referenced based singletons via the curiously recurring template pattern.

If you wish to create a singleton, simply:

  • inherit this class (supplying your class as the template parameter).

  • this class is declared a friend.

  • your class has a protected constructor.

  • use a macro as shown below to call it.

#define Test TestObject::instance()

...

cout << Test.value() << endl;

See also

src/test/singleton.cpp, Singletons

Public Functions

inline virtual ~Singleton()

Public Static Functions

static inline T &instance()

Stores a reference to a single instantiation of the template object. If the template class has a protected or private default constructor (no arguments), then it ensures the class will be a singleton itself.