 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lhyatt@princeton.edu Guest
|
Posted: Sun Jul 30, 2006 1:43 am Post subject: static initialization order issues |
|
|
Hi Everyone-
I am trying to work around some issues related to initialization order
of static template members. I know this order is unspecified, but I am
wondering if I can at least be sure it will happen after initilization
of constants. Here is what I mean:
--------------------------------
#include <string>
template<typename T>
struct A {
static const std::string s;
};
extern char const c[]="hello";
template<typename T>
const std::string A<T>::s(c);
int main(){}
------------------------------------
Is it guaranteed that c points to the string "hello" before A is
initialized? It's important that c[] be declared extern for the actual
application I need. (But does that matter anyway for this question?)
Thanks very much.
-Lewis
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Dave Steffen Guest
|
Posted: Tue Aug 01, 2006 12:29 am Post subject: Re: static initialization order issues |
|
|
If I may ask a follow-up question...
"kanze" <kanze@gabi-soft.fr> writes:
| Quote: | lhyatt (AT) princeton (DOT) edu wrote:
I am trying to work around some issues related to
initialization order of static template members.
[...]
Basically, initialization is divided into three steps: zero
initialization, static initialization and dynamic initialization.
These steps always occur in that order, over the entire program.
(In practice, both static initialization and zero initialization
take place on program loading, before the new process even starts.)
|
Consider a class that has several static member variables:
// foo.h
struct foo
{
static int a;
static double b;
static double c;
};
// foo.cpp
int foo::a = 5; // static initialization
double foo::b = 42 / 3.14159; // still static initialization?
double foo::c = pow(d, 2.0); // dynamic initialization?
// elsewhere, in a translation unit far, far away...
double x = foo::c;
Now, I'm pretty sure that b is guaranteed to be initialized before c is.
I'm pretty sure, but not completely positive, that b is statically
initialized (IIRC 42 / 3.14159 is a compile-time constant).
I'm pretty sure that c is _not_ guaranteed to be initialized before
main() is called.
My question is this: is c guaranteed to be initialized before it's
ever used? Is there a possibility that c hasn't been initialized
before x references it?
We just spent a day tracking down something of precisely this
nature, wherein x was being set to 0. Is this allowed? Or is this
a compiler issue?
Thanks very much.
----------------------------------------------------------------------
Dave Steffen, Ph.D.
Software Engineer IV Disobey this command!
Numerica Corporation - Douglas Hofstadter
dgsteffen at numerica dot us
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Dave Steffen Guest
|
Posted: Wed Aug 02, 2006 5:50 am Post subject: Re: static initialization order issues |
|
|
Curouser and curiouser. :-)
"kanze" <kanze@gabi-soft.fr> writes:
| Quote: | Dave Steffen wrote:
If I may ask a follow-up question...
"kanze" <kanze@gabi-soft.fr> writes:
[...]
Consider a class that has several static member variables:
// foo.h
struct foo
{
static int a;
static double b;
static double c;
};
// foo.cpp
int foo::a = 5; // static initialization
double foo::b = 42 / 3.14159; // still static initialization?
Yes. I don't think that the result is required to be the same
as if the expression were evaluated dynamically, however.
|
Uhh... ok, that hurt my head. Having read the the standard more
carefully, I think '42 / 3.14.159' is a constant expression; therefore
there's no reason for this _not_ to be static initialization. That
having been said, what other value could foo::b have?
Or is your point that if foo::b is dynamically initialized, there's
some (brief) period of time between when it's zero-initialized and
when it's dynamically initialized, and therefore some _other_ variable
(that is, not foo::b) might have a different value (e.g. 0)?
| Quote: | double foo::c = pow(d, 2.0); // dynamic initialization?
Definitly. As soon as a function call is present, the
initialization is dynamic. (Also, I presume you mean
pow(b,2.0). There is no d anywhere that I can see.)
|
Argh, yeah. The relevent part of the standard (3.6.2) has 'd1' and
'd2' and I got confused. Or rather, I was already confused about the
static initialization, and got more confused. :-)
(See also the discussion with Matthias Hofman and Peter Dimov titled
"Confusion about 3.6.2/2")
| Quote: | // elsewhere, in a translation unit far, far away...
double x = foo::c;
[...]
My question is this: is c guaranteed to be initialized
before it's ever used?
Of course not.
|
Right, sorry, let me rephrase that question.
3.6.2 para 3 says, in part, "If the initialization is deferred to some
point in time after the first statement of main, it shall occur
before the first use of any function of object defined in the same
translation unit..."
So, given a header file with
double foo();
and a corresponding C file that says:
const double a = 42; // static initialization
const double b = sqrt(a); // dynamic initialization
double foo() { return b; }
I don't think it's legal for foo() to ever return 0. My question is,
am I right?
----------------------------------------------------------------------
Dave Steffen, Ph.D.
Software Engineer IV Disobey this command!
Numerica Corporation - Douglas Hofstadter
dgsteffen at numerica dot us
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|