Boost logo

Boost :

Subject: Re: [boost] [parameter] aux_/void.hpp: potential undefined behavior
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-11-04 07:47:01

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;

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.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP


IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at