The following seems to be the recommended way to define a singleton in C++:
class Singleton {
private:
Singleton();
public:
static Singleton & get_instance() {
static Singleton instance;
return instance;
}
~Singleton() {
// destructor
}
Singleton(Singleton const&) = delete;
void operator=(Singleton const&) = delete;
}
Now, take this function:
void foo() {
Singleton & s = Singleton::get_instance();
}
I expected the destructor to be called when the singleton instance goes out of scope in that function, but it's not. When does the destructor get called?