Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2001-11-13 14:53:53


> How about a singleton class for boost? This is one of the most simple
> design patters, and can rather easily be implemented using templates. Just
> as the smart pointer, there may be a need for different variants, to
> handle the lifetime of the singleton in different ways.

Hmmm, I have a singleton class in pool. Someone on this list recently gave
me a new idea on how to do it better; I'm planning to do this soon.

My current singleton:
  . Allows access during dynamic initialization (i.e., before main()).
  . Provides thread-safe construction at the time of its first access.
  . Guarantees destruction order: singletons act as objects with static
duration.
With the following restriction:
  . Only one thread may be running before main() begins or after main()
ends.

The new idea I referred to above will remove that restriction, at which
point I intend to offer the pool singleton class as a generic singleton
solution.

Question on your code: what exactly are you doing in the constructor?
    singleton()
    {
        assert(instance_==0);

        char* p=reinterpret_cast<char*>(this);
//I don't see how the following line has any effect
        p-=(int)(singleton*)(T*)(1) - 1;
        instance_=reinterpret_cast<T*>(p);
    }

        -Steve


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