Boost logo

Boost :

Subject: Re: [boost] Review Request: Singularity
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2011-08-24 09:18:53


On 24/08/2011 04:28, Ben Robinson wrote:
> Mathias,
>
> I think this discussion is getting a little off track. There is a lot
> of literature on creating a thread-safe Singleton that manages the lifetime
> of your object according to your needs. However, there is no silver bullet
> for the pattern. Different variants make different tradeoffs depending on
> the user's needs. The three issues that are inherent to Singleton are:
>
> 1) You have little control over when the Singleton is destroyed. Should you
> have multiple Singleton which depend on each-other, very often you need a
> very specific teardown order.

It should be the inverse order of the construction order.
Any system that requires to explicitly give both construction and
destruction order without automatically deducing one from the other is
IMO broken.

function-level statics already deal with this.

> 2) The instance() member is global, so you cannot fully encapsulate it
> inside the classes that need it. Singularity allows you to optionally
> expose global access to the class, should you desire context independent
> objects.

The code is two lines of straightforward C++. You're free to encapsulate
it the way you want. That's easier than dealing with the idiosyncrasies
of a library.

> 3) Singleton restricts you to a single constructor, usually the default one.
> Singularity allows you to use any/all of the constructors of your object.

It restricts you to whatever you choose to write your function as.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk