Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-10-06 06:23:55


Bronek Kozicki wrote:
> What's wrong with simple:
>
> template <typename T> struct wrapper
> {
> static T data;
> };
>
> template <typename T>
> T wrapper<T>::data;
>
> template<C> void foo(C arg)
> {
> bar<C>& abar = wrapper<bar<C> >::data;
> }
>
> ... which will trigger static (thread-safe) initialization.

Nothing. I like this very much! Funningly, I used this already
under the hoods in my once_class without noticing its potential.
I also vaguely remember having seen somethin similar from
Slapeta who called the wrapper static_< >. Again without noticing
that it is a potential solution to fixing the spirit bug.

Some notes though:
1) Lazy initialization on first use is not possible with this
    approach.
2) It does not address the question of how to construct the static
    if it is being used from another static ctor run. (I know that
    this isn't recommended practice anyways, but a library implemntor
    might really need this behaviour.)

Perhaps both wrappers should be available to give the user a choice?
Would it be possible to wrap both under a common interface?

3) class_once also in my current implementation has a problem
    with exceptions, thrown by ctors. once_call as I found out
    shouldnt throw (from a comment of Bill Kempf). I am considering
    calling terminate in this case. Reasonable?

Regards,
Roland


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