Advertisement

Annoying warning: 'x' intialized after 'y'

Started by April 16, 2005 11:40 PM
9 comments, last by Sharlin 19 years, 7 months ago
I'm moving a project from VC7 to GCC under Mac OSX, and I'm getting a bunch of the warning that says some variable will be initialized after some other... caused when you have a constructor initializer list with variables in a different order from that in which they're declared in the class declaration, ie:

class A
{
public:
  A();

  int mX;
  int mY;
};

A::A() : mY(0), mX(0)
{
}
The above is the type of situation that causes the warning. Is there a flag to disable this warning? I couldn't see anything in the gcc man file or the Xcode build options. I could just switch around the initializations so they're in the right order, but that's a little annoying since there's kind of a lot of code, and I know the order in which the initializations will be run with or without the warning.
According to the GCC man-page -Wreorder turns this warning on. What you want then is -Wno-reorder.
Advertisement
This is mentioned in Scott Myers excellent 'Effective C++'. There's a good reason for this to happen and I think the standard specifies the order of items in the initialiser list should match the order they are declared in the class. I don't have the book to hand so I can't tell you what that reason is.

Skizz
Thanks for the info, I'll use that option now.

Skizz,

I believe the standard says that the order of initialization will match the order of declaration in the class declaration, regardless of what order you type the members in the initializer list.
Turning off warnings is usually bad. Unless you have to ship tomorrow or something then IMHO you should just take the hit now and fix the code. The warning is there because it's fairly common for people to (wrongly) assume that members are initialized in the order given in the initializer list and then they end up with screwed dependencies and random heap corruption.

Turning off the warning will fix the immediate pain but the next guy that comes along is going to hate you after he spends a week debugging something that would have been found the first time the code was compiled if the warning wasn't turned off.
-Mike
I'm with Anon Mike on that one.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote: Original post by Anon Mike
Turning off warnings is usually bad. Unless you have to ship tomorrow or something then IMHO you should just take the hit now and fix the code. The warning is there because it's fairly common for people to (wrongly) assume that members are initialized in the order given in the initializer list and then they end up with screwed dependencies and random heap corruption.

Turning off the warning will fix the immediate pain but the next guy that comes along is going to hate you after he spends a week debugging something that would have been found the first time the code was compiled if the warning wasn't turned off.

You say that turning off warnings is bad. However, what about (in my eyes) useless warnings such as 4786 I think it was, that function names are too long, and are truncated to 255 characters in debug mode (happens a lot when passing stl containers around)? Surely it's not going to kill anyone to turn that sort of thing off?

Qoy: Is it that much trouble to initialize mX before mY? It will make things clearer for people looking through your code, as well as avoiding the aformentioned warning?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote: Original post by SirLuthor
You say that turning off warnings is bad. However, what about (in my eyes) useless warnings such as 4786 I think it was, that function names are too long, and are truncated to 255 characters in debug mode


Warnings are good. VC6 was not. [smile]

(it's type names, not function names)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote: Original post by Fruny
Warnings are good. VC6 was not. [smile]

(it's type names, not function names)

Hehehe, yea, I probably should upgrade [smile]
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote: Original post by SirLuthor
You say that turning off warnings is bad. However, what about (in my eyes) useless warnings such as 4786 I think it was, that function names are too long, and are truncated to 255 characters in debug mode (happens a lot when passing stl containers around)? Surely it's not going to kill anyone to turn that sort of thing off?

The big difference between a warning for initializers being in the wrong order and a warning for debug symbols being to long is that the former is fixable via trivial code change. The latter is not fixable at all because you're running up against an inherent limitation of the compiler, not the language.

You have to use your judgement for these kind of things. I turn off a very few selected warnings myself - "unreferenced inline function" being my favorite. If you use non-standard compiler-specific features then you might need to turn off portability warnings about those - I use zero-length arrays in structures all the time for example and have to turn off warnings about those.
-Mike

This topic is closed to new replies.

Advertisement