Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-01-20 08:57:24


On Thu, Jan 20, 2005 at 02:11:35PM +0100, Stefan Slapeta wrote:

> Jonathan Wakely wrote:
>
> >
> >No. Unless the member is instantiated it will not be initialised. Since
> >whether it is instantiated depends on whether you use it, which might
> >depend on whether you've got testing/loggin/debugging code turned on,
> >to guarantee that the member is initialised even when not used you might
> >have to explicitly instantiate it for each specilalisation:
> >
> >template const int static_<int>::member = 0;
> >template const bool static_<bool>::member = true;
> >
> >
>
> If you ever access the member by calling static_<A>::get() in some piece
> of _instantiated code_ it's guaranteed that static_<A>::member is going
> to be instantiated (at program startup).

Yeah.
I guess I was talking more about the general case, not your specific
static_ class. For a more complicated class you could instantiate the
class but that static member will not get instantiated unless used.
For static_ it's pretty hard to instantiate it and not cause
instantiation of the member too :-)

For more complex code, where member might only get used in a debug
build, you can't rely on it being instantiated in other builds.

> Of course, this is not the case for e.g. function templates that are
> never instantiated or for inline functions that are not actually called.
> However, I think it's rather a feature than a bug not to instantiate a
> static_<A> member if the code is never executed.

Yes, I agree. I was just pointing out that behaviour and that you can't
rely on static members of templates always being instantiated.

If I've just confused the issue I apologise, for anyone unclear what I
mean the attached file shows that although A<int> and A<char> are both
instantiated, only A<int>::member is instantiated.

jon




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