Re: initialisation orders of global/namespace/class static objects.
On Oct 26, 7:42 am, Taras_96 <taras...@gmail.com> wrote:
On Oct 23, 8:18 pm, James Kanze <james.ka...@gmail.com> wrote:
On Oct 23, 6:39 am, Taras_96 <taras...@gmail.com> wrote:
A question about initialisation orders of
global/namespace/class static objects.
In "the c++ programming language", Bjarne states that:
page 217
"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (=A710.4.9). If such a variabl=
e
has no explicit initializer, it is by
default initialized to the default for its type (=A710.4.2). The defa=
ult
initializer value for builtin
types and enumerations is 0 . For example:"
But 10.4.9 yields:
"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."
So are nonlocal objects initialized in the order in which they
were declared, or defined? These two statements seem to
disagree, and I couldn't find anything in the errata for the
book.
I suspect it's just a little bit of careless language on the
part of Bjarne, because initialization in the order of
declaration isn't possible. (More generally, don't expect
the same degree of precision in a pedagogic text as in the
standard. Bjarne also fails to mention zero initialization
and static initialization in the passage you quote; his goal
is to explain, not to specify in exact detail.)
I couldn't find an answer in the Standard:
The standard states that zero-initialisation is done
before dynamic initialisation of nonlocal objects (with a
couple of exceptions with namespace and local statics) -
Stmt.dcl/4 However, I couldn't find anything about the
initialisation order (static or dynamic) of nonlocal
objects
=A73.6.2 [basic.start.init] talks about nothing else.
I must have been reading an old copy, 3.6.2 yields:
I think, rather, that you're reading a draft for the next
version of the standard. Almost everything which concerns
ordering has been reworded to take threading into account. The
basic effects should not have changed, however.
"Dynamic initialization of an object is either ordered or unordered.
Definitions
of explicitly specialized class template static data members have
ordered initialization. Other class template
static data members (i.e., implicitly or explicitly instantiated
specializations) have unordered initialization. Other objects
defined in namespace scope have ordered initialization. Objects
defined within a single translation unit and with
ordered initialization shall be initialized in the order of their
definitions in the translation unit. The order of initialization
is unspecified for objects with unordered initialization and for
objects defined in different translation units. [ Note: 8.5.1
describes the order in which aggregate members are initialized. The
initialization of local static objects is described in
6.7. =97end note ]"
I don't really understand the reference to class template
static data members, but I'm happy leaving that for now.
I think I understand it, and I don't particularly like its
implications. Ordered, here, means that it is guaranteed that
one operation will take place before the other; that no external
synchronization is needed for them. It sounds to me like this
is saying that instantiations of static data members of template
classes might need some sort of external synchronization if they
access anything else.
The excerpt states that objects defined in namespace scope
have ordered initialization, but doesn't mention global scope,
and whether their initializations are ordered...
"Global scope" is a short name for "global namespace scope";
it's a namespace scope. But attention about what is meant by
"ordered": ordered just means that one takes place before the
other; that you don't have to worry about synchronization issues
in constructors. In this case, the key is that the order is
unspecified if the objects are defined in different translation
units. (And IMHO, instantiations of template data members
should be ordered, but with an unspecified order with regards to
all other static data, regardless of translation unit.)
--
James Kanze