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 misunderstandin g 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 2202
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 misunderstandin g of the language on my part?
Yes, it's a misunderstandin g. 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 misunderstandin g of the language on my part?
Yes, it's a misunderstandin g. 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 am
seeking to determine whether I am programming non-standard C++ or if the
problem lies elsewhere.
To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const...
|
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 one do this?
|
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
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
|
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
(<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>)
The FAQ section describes a solution using methods returning references to
static objects.
But consider:
| |
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, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
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 entry point and the calling of
main().
C.) main().
So, if the above is OK, does static initialization occur during A or B? What
happens during A?
|
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
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |