http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18635
Ádám Rák <adam.rak at streamnovation dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |adam.rak at streamnovation
| |dot com
--- Comment #8 from Ádám Rák <adam.rak at streamnovation dot com> 2010-11-26
00:30:17 UTC ---
in g++-4.6 (and maybe all before) this bug can be even more troublesome:
struct AA
{
int &a;
AA() : a(a)
{
}
};
int main()
{
AA aa;
cout << &aa.a << endl;
return 0;
}
compiled without a warning even with
g++ main.cpp -O3 -Wall -pedantic -Wextra -Winit-self -Wuninitialized
And in -O0 it prints some address, probably the address of the reference as
suggested before. But in -O1..3 it prints a 0, which means we made an
nullreference.
The practical problem is that because of this, the code can be easily messed up
like this:
class AA
{
...int &aaa;
AA(int& aaaa) : aaa(aaa) {...
A single typo and the compiled does really strange things, the segfault is best
case, sometimes the reference points a valid address. It is very hard to debug
too. And when the programmer checks the code he/she can naively think that the
compiler should check it, so "why bother checking whether they are spelled
exactly the same?"
The old testcase was a bit harder to do accidentally, this one can happen more
easily. A self-init warning might enough to clue the programmer if this
happens. An error would be better if we are sure this is invalid.