Skip to content

Instantly share code, notes, and snippets.

@danlark1
Created January 11, 2020 20:48
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