Warn class members about self-initialization

Take a look at this piece of C ++ code:

class Foo { int a; public: Foo(int b): a(a) {} }; 

Obviously, the developer wanted to initialize a with b , not a , and this is a pretty difficult mistake.

Clang ++ will warn about this possible error, while GCC will not, even with additional warnings enabled:

 $ clang++ -c init.cpp init.cpp:5:27: warning: field is uninitialized when used here [-Wuninitialized] public: Foo(int b): a(a) {} ^ $ g++ -Wall -Wuninitialized -Winit-self -c init.cpp $ 

Is it likely to include the same output for g ++?

+4
source share
1 answer

Use the new gcc :-) Everything seems to be in order:

 stieber@gatekeeper :~$ g++ -Wall -Wuninitialized -Winit-self -c Test.cpp Test.cpp: In constructor 'Foo::Foo(int)': Test.cpp:5:9: warning: 'Foo::a' is initialized with itself [-Wuninitialized] stieber@gatekeeper :~$ gcc --version gcc (Debian 4.7.1-2) 4.7.1 
+8
source

All Articles