|
Boost : |
From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2005-01-19 13:31:54
Stefan Slapeta wrote:
> 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.
>
This is a really nice trick!
However a question:
Is it guaranteed by the standard, that the ctor of the "member" is
beeing called at program startup?
Perhaps thread_specific_ptr itself should be implemented by means of
this idiom then?
Roland
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk