Boost logo

Boost :

From: Jorge Lodos (lodos_at_[hidden])
Date: 2005-02-14 16:54:38

> -----Original Message-----
> From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
> On Behalf Of Peter Dimov
> Sent: Monday, February 14, 2005 2:38 PM
> To: boost_at_[hidden]
> Subject: Re: [boost] singleton usage

> A singleton is simply a glorified wrapper over several global variables
> and
> functions. There is little difference between
> Singleton::instance().f();
> and
> Singleton::f();
> where the second Singleton is a namespace. The global variables don't
> become
> any less "evil" when they are made members of a singleton class.
> When a class represents an out-of-program entity that, by its nature, has
> a
> single instance, this class can be flattened into functions.
> This isn't black and white, of course; today's code may treat the video
> card
> as a singleton, but tomorrow's computers may add multiple video card
> support. In this case I still prefer something along the lines of
> VideoCard & get_video_card( int id ); // noncopyable
> or maybe even
> video_card_ref get_video_card( int id ); // reference copy semantics
> instead of the "multisingleton" way of
> VideoCard::instance( id ).
> But "true" singletons are either crippled ordinary classes or collections
> of
> global functions (under the assumption that there are no initialization
> order issues, of course).

Thank you for your comments. I agree with everything you said and try to
avoid singletons in the first place. However, I have a practical problem
that I have been able to "solve" only thru singletons.
In a header only library that uses a class factory, the registration of
objects with the factory is performed in a constructor of an object inside
an anonymous namespace. Since the header may be #included more that once, I
made those objects singletons to avoid multiple registration. The
singleton::instance method may be called more that once without need, that's
why I put "solve" in quotes. Since the object is inside an anonymous
namespace, I can not use templates, which never get instantiated.
If you or anyone else have a suggestion that allows for automatic
registration without using singletons please share it :-) I would like to
keep the anonymous namespace, but if there is a nice solution that requires
a named one then I would adopt it.
Thanks in advance.

Best regards

Boost list run by bdawes at, gregod at, cpdaniel at, john at