Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-08-13 08:19:51


From: "Michael Kenniston" <Msk_at_[hidden]>

> John Max Skaller and Bill Kempf wrote:
> > > I believe every single use of static variables
> > > and singletons is BAD DESIGN.
> > Well, I don't agree. <snip> there are several
> > classes of designs that simply can't be coded with out the use of
> > global data.
> > > Show me one. I've never seen one. I've never used
> > > global data (not since the '70's anyhow).
> > > [lexical scanner example snipped]
>
> While I don't have enough experience with singletons to
> convincingly argue either side, I do have enough experience
> to find this discussion very interesting, especially when it
> includes specific examples like the lexical scanner.

There is one important thing to note here; the definition of "global data"
(static data, singleton) matters. "Global data is evil" refers to
unrestricted communication through shared data, not to the lifetime of the
object. In

static int y;

int main()
{
  int x;
}

'x' and 'y' are 'singletons' by the lifetime definition, but they are not
'global data' by the communication definition, since the rest of the code
doesn't see them.

Another non-evil singleton example is std::cout; code rarely communicates
through cout's state. :-)

--
Peter Dimov
Multi Media Ltd.

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