Skip to content

Instantly share code, notes, and snippets.

@danlark1

danlark1/singleton.h

Created Jan 11, 2020
Embed
What would you like to do?
Singletons
// Very bad because the destruction order is not specified.
// From experience, these are one of the hardest to debug bugs.
template <class T>
T BadSingleton() {
static T t;
return t;
}
// Better. It is NOT a leak because the compiler will clear it after the shutdown.
// Downside -- destuctors are not called. But you should never do complicated
// things in destructors.
template <class T>
T BetterSingleton() {
static T* t = new T();
return *t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.