Shot myself in the foot, but gcc provided the bullets!

This is a discussion on Shot myself in the foot, but gcc provided the bullets! within the C++ Programming forums, part of the General Programming Boards category; Alternate title: "Another great candidate for a warning message"
So I was testing a program and noticed some really weird ...

Shot myself in the foot, but gcc provided the bullets!

Alternate title: "Another great candidate for a warning message"

So I was testing a program and noticed some really weird behavior. It was acting as if some boolean values had been set to 'true', though I was certain that they had not been. Incredulous, I decided to print them out. To my suprise, the output was something like:

96
34
71

I started to panic - surely this was a sign of major stack corruption! Just as I was about to fire up the debugger I decided to recheck the class, just for good measure. Then I spotted it:

Code:

struct foo
{
foo( bool enable )
: enabled( enabled )
{ }
// ...
};

Ack - I was initializing the member with itself!

I wonder why gcc didn't issue a warning, though? I mean, when would you *ever* need to do such a thing, anyway? Let's see...never! I've got just about every warning turned on, too, even -Wunused (which should have been triggered, I would think).

Ok, so this is weird. If I use the -Wunused switch, no warning, but with -Wextra (with or without -Wunused), I get an unused variable warning!

I'm still going to look around to see if I can get it to detect self-initialization, though.

EDIT: Well, I've gone through all of the *documented* switches, but didn't find any that would help. Next step I guess would be to check the undocumented ones. At any rate, it probably should have tripped on -Winit-self, so I may need to report it to the mingw bugtracker anyway...

GCC only enables its flow analyzer if it optimizes at all. So if you use -O0 or no -O option, you don't get a flow analyzer. Without a flow analyzer, you don't get warnings about uninitialized variables. (As well as some other issues, such as aliasing violations.)