Boost logo

Boost :

Subject: Re: [boost] Is there any interest in a base class which prevents construction during the static initialization phase?
From: Ben Robinson (icaretaker_at_[hidden])
Date: 2011-06-14 00:22:17


>
> On Sun, Jun 12, 2011 at 4:51 PM, Ben Robinson <icaretaker_at_[hidden]>
> wrote:
> > Hello,
> >
> > Because the order of static initialization is undefined, numerous
> difficult
> > to detect problems can arise when multiple objects are initialized during
> > the static initialization phase. ?This base class would guarantee that a
> > class will never be unintentionally constructed during static
> > initialization. ?This is the same philosophy behind how
> boost::noncopyable
> > guarantees that a class will never be unintentionally copied.
>
> What you probably mean is "initialization of objects with static
> storage duration", which can be static and dynamic. All such objects
> are first zero initialized. Then the ones that are initialized with a
> constant expression are initialized, and this concludes the static
> initialization phase. Then the dynamic initialization phase follows.
>
> Only the dynamic initialization phase can cause problems.
>
> However, the dynamic initialization phase can cause problems with any
> object that uses it. That is, whether or not a compile error is
> induced should only depend on whether or not a given object with
> static storage duration is initialized during the dynamic
> initialization phase. Deriving or not deriving from
> nonstaticinitializable doesn't seem to have anything to do with this.
>
> Don't get me wrong, I agree that using objects with static storage
> duration that are initialized dynamically is the source of many
> problems, but it seems like it makes more sense to detect and report
> such initialization with a lint-type utility rather than relying on a
> programmer to derive from a type.
>
> Emil Dotchevski
> Reverge Studios, Inc.
> http://www.revergestudios.com/reblog/index.php?n=ReCode
>
> Emil,
>

Emil,

Thank you for your feedback. You are correct in that I am referring to
objects with static storage duration that are initialized dynamically. No
compile error is generated if you unintentionally create an object this way,
when you inherit from the proposed base class. Instead, the program will
assert before nonstaticinitializable::enable_initialization() is called
(which should be called as the first line of C++ code), usually in main().

Detecting this unintended behavior via an assert() before main(), is greatly
preferred to debugging undefined behavior. In this way, both noncopyable,
and nonstaticinitializable communicate intent to the compiler/program, which
can then prevents unintended behavior, an objectively desirable quality.
 Lint like tools are always available, however I thought to detect this
unintended behavior directly in the language, without requiring an
additional platform specific tool.

Since this base class prevents creating an "object with static storage
duration that is initialized dynamically", I would welcome improved naming
suggestions. :)

Thank you,

Ben Robinson


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