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 4 2097
Bret Pehrson wrote: 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
Victor Bazarov wrote: Bret Pehrson wrote: 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/ )
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.
Bret Pehrson wrote: Victor Bazarov wrote:
Bret Pehrson wrote:
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!
[...]
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
Victor Bazarov wrote: Bret Pehrson wrote: Victor Bazarov wrote:
Bret Pehrson wrote:
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! [...]
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.
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"?
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I...
|
by: JustSomeGuy |
last post by:
I have a class that has a static member variable.
string x;
x should never change during use and should be intialized to "abcd".
How does...
|
by: JKop |
last post by:
What's the difference between them?
Take the following:
#include <iostream>
struct Blah
{
int k;
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that...
|
by: Steve Folly |
last post by:
Hi,
I had a problem in my code recently which turned out to be the 'the "static
initialization order fiasco"' problem...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts,...
|
by: JohnQ |
last post by:
The way I understand the startup of a C++ program is:
A.) The stuff that happens before the entry point.
B.) The stuff that happens between the...
|
by: copx |
last post by:
I don't know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What...
|
by: akomiakov |
last post by:
Is there a technical reason why one can't initialize a cost static non-
integral data member in a class?
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |