On 11 Jan 2006 13:58:29 -0800, "Luke Meyers" <n.luke.meyers@gmail.com>
wrote:
[color=blue]
>I just spent two and a half days chasing down a bug resulting from
>initializing a reference to itself, like so:
>
>struct Bar;
>struct Foo {
> Bar& bar;
> Foo() : bar(bar) {}
>};[/color]
It's about time that you got a copy of the C++ standard. <g>
[color=blue]
>The compiler I was using (gcc 3.2.3) issued no warning for this. I
>assume that any use of bar produces undefined behavior. If this is the
>case, then it seems to be a defect in the compiler not to issue a
>warning. Is my analysis correct? Specifically:
>
>(1) Is there any conceivable circumstance in which this will produce
>anything but undefined behavior?[/color]
No. See 8.3.2, par. 4. A reference must be initialized with a "valid
object or function". Therefore, a reference cannot be initialized with
itself.
[color=blue]
>(2) is there any reason why a compiler would have difficulty detecting
>this, in the general case?[/color]
I'm not sure whether a compiler diagnostic is required here, but it is
UDB in any case. Note that a compiler vendor (or implementation) is
not necessarily required to issue a warning or error when your code
induces UDB.
[color=blue]
>(3) Anyone happen to know if later versions of gcc, or other compilers,
>address this?
>
>Please note that while I'm referring to a specific compiler here, my
>question (at least, part (1)) is really about language behavior, hence
>not (IMHO) OT.
>
>Luke[/color]
--
Bob Hairgrove
NoSpamPlease@Home.com