Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rules concerning static initialisation

0 views
Skip to first unread message

matt

unread,
Jul 2, 2009, 9:49:14 PM7/2/09
to
What are the rules concerning static initialisation of intrinsic
types?

I have read example code of the form

namespace
{
bool register()
{
// blah - do something useful in here
}

bool registered = register();
}

used to ensure that something is done at program startup and have
played with it a bit. If I attempt to move this and associated code
into a library and link to it it does not get called. What are the
rules concerning initialiation of types and where can I find em?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

litb

unread,
Jul 3, 2009, 12:29:09 AM7/3/09
to
On 3 Jul., 03:49, matt <mattjb...@ntlworld.com> wrote:
> What are the rules concerning static initialisation of intrinsic
> types?
>
> I have read example code of the form
>
> namespace
> {
> bool register()
> {
> // blah - do something useful in here
> }
>
> bool registered = register();
>
> }
>
> used to ensure that something is done at program startup and have
> played with it a bit. If I attempt to move this and associated code
> into a library and link to it it does not get called.

This is because the Standard allows dynamic initialization for an
object to happen even after main is called. The first use of a
function or object that is defined in that object's translation unit
will trigger the initialization - before the use is actually done.
This is valid for all objects of namespace scope.

Note that i sent a defect report about that wording: It seems to
intend to allow the same for objects of class scope, but the wording
currently doesn't. Still, your environment would behave as if the
standard allows it, i suppose :) So you couldn't just stuff the
variable as a static data member of a class :)

> What are the rules concerning initialiation of types and where can I find
em?
>

You can find them in 3.6.2 "Initialization of non-local objects" in
the Standard, if you are concerned about this scenario.

Note that "static initialization" can refer to two things:
initialization of static-storage objects, and static initialization of
such objects. The latter is one way of initialization of static
objects, intended to be used for things that don't need to run code
(like, initialization with constant values). The other way is dynamic
initialization, which is the case in your example (because of the
function call).

Static initialization in your example *may* apply, but it doesn't have
to. If the compiler can compute that the function always returns true/
fase, and can compute that the static initialization satisfies some
other properties (no change of other statics, ...), it can actually do
static initialization for it. I suspect in your code, this isn't the
case, anyway.

Nick Hounsome

unread,
Jul 3, 2009, 12:09:32 PM7/3/09
to
On 3 July, 02:49, matt <mattjb...@ntlworld.com> wrote:
> What are the rules concerning static initialisation of intrinsic
> types?
>
> I have read example code of the form
>
> namespace
> {
> bool register()
> {
> // blah - do something useful in here
> }
>
> bool registered = register();
>
> }
>
> used to ensure that something is done at program startup and have
> played with it a bit. If I attempt to move this and associated code
> into a library and link to it it does not get called. What are the
> rules concerning initialiation of types and where can I find em?

The "intrinsic types" part is misleading here.
There are two issues:

Firstly, unlike initialisation with a constant which is done at
compile time, this requires runtime initialisation in code before main
().

The second issue is to do with libraries and the C++ standard does not
deal with libraries or linking (at least beyond the level of
'compilation unit').
In practice, linkers only pull in object files from libraries to
satisfy references to symbols from already linked code. Since there is
no reference to anything in the compilation unit including the example
code the linker sees no need to include it and hence no need to
include code to do the initialisation.
To get around this you must include the .o file directly in the link
command rather than via a library (unless your linker has an option to
force linking of the entire library)

Francis Glassborow

unread,
Jul 3, 2009, 12:19:13 PM7/3/09
to
matt wrote:
> What are the rules concerning static initialisation of intrinsic
> types?
>
> I have read example code of the form
>
> namespace
> {
> bool register()
> {
> // blah - do something useful in here
> }
>
> bool registered = register();
> }
>
> used to ensure that something is done at program startup and have
> played with it a bit. If I attempt to move this and associated code
> into a library and link to it it does not get called. What are the
> rules concerning initialiation of types and where can I find em?

If the above was exactly what you wrote then you have a problem because
'register' is a keyword. I am a little surprised that the compiler
allows you the declaration of 'registered' (I am less surprised by it
allowing you the first line because I think it is treating that as the
declaration of an unnamed, default initialised bool but that is purely a
guess as you wrote code that the compiler was probably unprepared for.

0 new messages