Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Rules concerning static initialisation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
matt  
View profile  
 More options Jul 2, 9:49 pm
Newsgroups: comp.lang.c++.moderated
From: matt <mattjb...@ntlworld.com>
Date: Thu, 2 Jul 2009 19:49:14 CST
Local: Thurs, Jul 2 2009 9:49 pm
Subject: Rules concerning static initialisation
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! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
litb  
View profile  
 More options Jul 3, 12:29 am
Newsgroups: comp.lang.c++.moderated
From: litb <Schaub-Johan...@web.de>
Date: Thu, 2 Jul 2009 22:29:09 CST
Local: Fri, Jul 3 2009 12:29 am
Subject: Re: Rules concerning static initialisation
On 3 Jul., 03:49, matt <mattjb...@ntlworld.com> wrote:

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! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Hounsome  
View profile  
 More options Jul 3, 12:09 pm
Newsgroups: comp.lang.c++.moderated
From: Nick Hounsome <nick.houns...@googlemail.com>
Date: Fri, 3 Jul 2009 10:09:32 CST
Local: Fri, Jul 3 2009 12:09 pm
Subject: Re: Rules concerning static initialisation
On 3 July, 02:49, matt <mattjb...@ntlworld.com> wrote:

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)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Glassborow  
View profile  
 More options Jul 3, 12:19 pm
Newsgroups: comp.lang.c++.moderated
From: Francis Glassborow <francis.glassbo...@btinternet.com>
Date: Fri, 3 Jul 2009 10:19:13 CST
Local: Fri, Jul 3 2009 12:19 pm
Subject: Re: Rules concerning static initialisation

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.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google