Boost logo

Boost :

Subject: Re: [boost] [parameter] aux_/void.hpp: potential undefined behavior
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-11-04 09:51:30


2011/11/4 Stewart, Robert <Robert.Stewart_at_[hidden]>

> Krzysztof Czainski wrote:
> > 2011/11/3 Stewart, Robert <Robert.Stewart_at_[hidden]>
> > > Krzysztof Czainski wrote:
> > > >
> > > > struct void_ {};
> > > >
> > > > namespace aux
> > > > {
> > > > inline void_& void_reference()
> > > > {
> > > > static void_ instance; // (1)
> > > > return instance;
> > > > }
> >
> > don't you think it's better to use static initialization? Or
> > maybe it doesn't matter?
> >
> > I mean, if the (empty autogenerated) default constructor is
> > called, then the compiler needs to generate some flag and an if
> > condition, so the default constructor is only called during the
> > first invocation of void_reference(). Of course, that can be
> > optimized away, but isn't it just better to use static
> > initialization here? (something like prefere pass-by-const-ref
> > to pass-by-value)
>
> I presume you mean the following:
>
> namespace aux
> {
> inline void_ & void_reference()
> {
> static void_ = {};
> return instance;
> }
> }
>

Yes.

> My reading of 6.7/4 in N3092 suggests that would permit initialization
> without the need to introduce a first call test since there is no declared
> default constructor and using an initializer eliminates the default
> constructor invocation that would lead to its definition.
>
> If we're interpreting things correctly, then, yes, this is a worthwhile
> tip to capture, especially in light of C++11's requirement for thread-safe
> initialization of function local statics.

Then I'm glad to have suggested something sensible ;-)

Regards,
Kris


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