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