Initialization of static members/constants???

B

Bret Pehrson

I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;


// elsewhere (other translation unit)
double m = Masses::mass3; // m and Masses::mass3 == 0!


If I change mass3 initialization to a non-arithmetic literal, all works; leave
as arithmetic operation, and the value is set to 0.

So, my question is this: does the language mandate the initialization order of
such constants? I thought that it was based on the declaration order in the
class definition, but if that is the case, then mass3 should have a non-zero
value.

Is this a problem w/ my compiler (MSVC 2003), or a misunderstanding of the
language on my part?

FWIW: I also tried using static const in a namespace instead of as class
members, same behavior.

Thanks
 
V

Victor Bazarov

Bret said:
I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;


// elsewhere (other translation unit)
double m = Masses::mass3; // m and Masses::mass3 == 0!


If I change mass3 initialization to a non-arithmetic literal, all works; leave
as arithmetic operation, and the value is set to 0.

So, my question is this: does the language mandate the initialization order of
such constants?

Only within the same translation unit.
I thought that it was based on the declaration order in the
class definition, but if that is the case, then mass3 should have a non-zero
value.

Is this a problem w/ my compiler (MSVC 2003), or a misunderstanding of the
language on my part?

Yes, it's a misunderstanding. Read about "static initialisation order
fiasco" in the FAQ ( http://www.parashift.com/c++-faq-lite/ )
FWIW: I also tried using static const in a namespace instead of as class
members, same behavior.

Placing them in a namespace instead of a class shouldn't really matter.

V
 
B

Bret Pehrson

Victor said:
Only within the same translation unit.


Yes, it's a misunderstanding. Read about "static initialisation order
fiasco" in the FAQ ( http://www.parashift.com/c++-faq-lite/ )

Yes, but these constants are declared/initialized in the same translation
unit. In my sample above, the .h represents 1 header, .cpp 1 source, and
elsewhere, is just the use of those class statics in some other context other
than the .cpp translation unit.

According to my understanding and what I've read, the SIOF only applies to
static initialization _across_ translation units, not within.

I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.
 
V

Victor Bazarov

[...]

Yes, but these constants are declared/initialized in the same translation
^^^^^^^^^^^^^^^
Uh, who is crazy here, I or you?
unit. In my sample above, the .h represents 1 header, .cpp 1 source, and
elsewhere, is just the use of those class statics in some other context other
than the .cpp translation unit.

According to my understanding and what I've read, the SIOF only applies to
static initialization _across_ translation units, not within.

So, if the 'm' object is in a different ("other") translation unit than
the 'Masses::mass3' object, would it make them "within" or "across"?
I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.

No, it's not a compiler bug.

V
 
B

Bret Pehrson

Victor said:
[...]

Yes, but these constants are declared/initialized in the same translation
^^^^^^^^^^^^^^^
Uh, who is crazy here, I or you?

Well, not me, but I'll admit to a poorly constructed sample -- see below.
So, if the 'm' object is in a different ("other") translation unit than
the 'Masses::mass3' object, would it make them "within" or "across"?

Ok, I see the point of confusion. m isn't another static, simply a local
variable. A more accurate complete snippet should be:

// assuming previous .h and .cpp snippet from op

// elsewhere (other translation unit)
int main()
{
double m = Masses::mass3; // m and Masses::mass3 == 0!
return 0;
}

This is what I intended to portray in my original sample, but didn't adequately
describe that.

I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top