I want to get some warnings if I use the following code:
class Danger
{
private:
int* buff;
public:
Danger( int& _in): buff(&_in) {}
int LetsCrash() { *buff=42; return 0;}
};
class DangerUser
{
private:
int otherVar;
public:
DangerUser( Danger& d): otherVar( d.LetsCrash()) {}
};
class Example
{
private:
int x;
DangerUser dangerUser;
Danger danger;
public:
Example( ):
dangerUser( danger ),
danger( x)
{}
};
int main()
{
Example ex;
}
The code compiles with
g++ -O0 -Wall -pedantic -Wextra x.cpp
and also with
clang++ -O0 -Wall -pedantic -Wextra x.cpp
without any warning but crashes as expected.
I use gcc in version gcc (GCC) 6.2.1 20160916 (Red Hat 6.2.1-2)
and also 6.1.0
. Clang comes in version clang version 3.8.0 (tags/RELEASE_380/final)
.
I already red Warnings for uninitialized members disappear on the C++11 but this points to a gcc bug which was already fixed in the versions I use.
Because the bug is trivial to detect ( using of a variable which is not constructed at the point of usage ) I wonder why none of my compilers I use detect that problems.
Remark: Compiling in -O0
is only necessary to get the crash in real life :-) Otherwise the bug is optimized out.
Is there maybe an additional -Wxyz
flag to get this warning alive?