Boost logo

Boost :

Subject: Re: [boost] [Boost.utility]
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-01-26 14:41:48


vicente.botet wrote:
>
> What about defining all the functions of the singleton at a
> namespace level. So rather than doing
>
> struct S : singleton<S> {
> void f1();
> int f2(char);
> };
>
> and use it as
>
> S::instance().f1();
>
> doing in this way
>
> namespace S {
> void f1();
> int f2(char);
> };
>
> and use it as
>
> S::f1();
>
> Of course, nothing forbids the implementation of the
> functions at the namespace level to use a singleton, but this
> is an internal detail, not shown at the user level.
>
> What use cases do you see where the class interface is more
> convenient than the namespace interface?

What you've shown is simply a variation of Monostate. What's missing is any opportunity to inline functions because the state must be in the implementation file. (The state could be put in an "off limits" nested namespace, but that's easy to violate, so a poor design.)

Aside from the possible loss of efficiency, there's no great disadvantage, though some Singleton designs allow for objects that are destroyed by atexit(), that can be resurrected if needed after being destroyed (due to another static scope object outliving the first instance), etc. Most of those things are overly complicated, however.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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