I'd use a singleton like this:
Singleton* single = Singleton::instance();
single->do_it();
I'd use an unnamed class like this:
single.do_it();
I feel as if the Singleton pattern has no advantage over the unnamed class other than having readable error messages. Using singletons is clumsier than using an unnamed class object: First, clients must first get a handle of the instance; second, the implementer of Singleton::instance()
might need to consider concurrency.
So why and how would you choose a singleton over an unnamed class?
As an addendum, although the obvious definition of an unnamed class might be
class {
// ...
}single;
I could as well define it like so:
#ifndef NDEBUG
class Singleton__ { // readable error messages,
#else
class { // unnamed, clients can't instantiate
#endif
// ...
}single;
with the latter approach having the advantage of readable compiler error messages but not being a singleton in debug mode.