Boost logo

Boost :

From: Stefan Slapeta (stefan_at_[hidden])
Date: 2005-01-18 05:53:44


Hartmut Kaiser wrote:

>
> Could you provide a patch?
>

I can ... a little static helper class is very helpful in this situation
(I posted it some time ago to the list):

////////////////////////////////////////
// class template static_
////////////////////////////////////////
template <typename T, typename OwnerT = void>
struct static_
{
     T* operator -> () const { return &member; }

     static T* get() { return &member; }

private:
     static T member;
};

template <typename T, typename OwnerT> T static_<T, OwnerT>::member;

///////////////

This way you can declare a global static instance of a type from
anywhere in the code, i.e. in header files, because templates show a
different behaviour for static members.

The code could look like

     typedef boost::thread_specific_ptr<ptr_t> tld_t;
     static_<tld_t, X> tld_helper_static;

     tld_t tld_helper = tld_helper_static.get();

     if (!tld_helper.get())
       tld_helper.reset(new ptr_t);
     ptr_t &helper = *tld_helper;

where X should denote any unique type (e.g. a private typedef) to
distinguish this static instance of tld_t from other static instances of
the same type.

Cheers,

Stefan


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