Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-09-26 09:08:10


"Schoenborn, Oliver" <Oliver.Schoenborn_at_[hidden]> writes:

> True but what about fundamental "types", like int and pointers? I'm
> pretty sure I read in Modern C++ Design that they are allocated and
> initialized as soon as TU available (i.e. upon startup if static
> link, or as soon as dso loaded if dynamically linked), and space
> available for TU lifetime:

That's not quite accurate.

>
> // SomeClass.hh
> class Foo {};
> template <typename TT>
> struct SomeClass {
> static Foo* foo;
> };
> template <typename TT>
> Foo* SomeClass<TT>::foo = NULL;
>
> // main.cc
> #include "SomeClass.hh"
> #include <iostream>
> int main() {
> SomeClass<bool> sc;
> // next line will ALWAYS print value of NULL to stdout:
> std::cout << (void*)sc.foo << std::endl;
> }

3.6.2 makes it pretty clear that there's no way to make use of the
cvery few guarantees that *are* given for the purposes we'd like:

3.6.2 - Initialization of non-local objects [basic.start.init]

-1- [core 151: The storage for Objects ] with static storage duration
 (basic.stc.static) shall be zero-initialized (dcl.init) before any
 other initialization takes place. Zero-initialization and
 initialization with a constant expression are collectively called
 static initialization; all other initialization is dynamic
 initialization. Objects of POD types (basic.types) with static
 storage duration initialized with constant expressions (expr.const)
 shall be initialized before any dynamic initialization takes
 place. Objects with static storage duration defined in namespace
 scope in the same translation unit and dynamically initialized shall
 be initialized in the order in which their definition appears in the
 translation unit. [Note: dcl.init.aggr describes the order in which
 aggregate members are initialized. The initialization of local static
 objects is described in stmt.dcl. ]

-2- An implementation is permitted to perform the initialization of an
 object of namespace scope with static storage duration as a static
 initialization even if such initialization is not required to be done
 statically, provided that

the dynamic version of the initialization does not change the value of
any other object of namespace scope with static storage duration prior
to its initialization, and the static version of the initialization
produces the same value in the initialized object as would be produced
by the dynamic initialization if all objects not required to be
initialized statically were initialized dynamically.

[Note: as a consequence, if the initialization of an object obj1
refers to an object obj2 of namespace scope with static storage
duration potentially requiring dynamic initialization and defined
later in the same translation unit, it is unspecified whether the
value of obj2 used will be the value of the fully initialized obj2
(because obj2 was statically initialized) or will be the value of obj2
merely zero-initialized. For example,

inline double fd() { return 1.0; }
extern double d1;
double d2 = d1; // unspecified:
                                  // may be statically initialized to 0.0 or
                                  // dynamically initialized to 1.0
double d1 = fd(); // may be initialized statically to 1.0

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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