Topic: initialising reference to itself


Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Sat, 27 Nov 2004 23:53:03 GMT
Raw View
ron@sensor.com (Ron Natalie) wrote in message news:<41a48364$0$1604$9a6e19ea@news.newshosting.com>...
> bill clarke wrote:
> > (this may be an FAQ; i couldn't find anything, but if it is, please
> > point it out)
> >
> > assuming there is no "i" in scope, should the following code be
> > accepted?  is it simply undefined behaviour?
> >
> > """
> > int &i = i;
> > """
> >
>
> "A reference shall be initialized to refer to a valid object..."
>
> Here i is initialized with it's own uninitialized self.   The program
> is ill-formed.

ill-formed means a diagnostic is required. I agree that with the
current wording, this is ill-formed. But was that intended? In general
that rule seems hard to diagnose.

Regards,
Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "bill clarke" <llib@computer.org>
Date: Tue, 23 Nov 2004 23:40:16 CST
Raw View
(this may be an FAQ; i couldn't find anything, but if it is, please
point it out)

assuming there is no "i" in scope, should the following code be
accepted?  is it simply undefined behaviour?

"""
int &i = i;
"""

i was unable to find anything in the standard that might disallow this.
should it be disallowed?  (it might be useful to use i in the
right-hand-side, for e.g., template/overload resolution)

testing the following code with several compilers produced some
diagnostics, while the executable produced core dumps or weirdness (as
you would expect):

"""
#include <cstdio>
int main() {
int &a = a;
std::printf("a = %d\n", a);
return 0;
}
"""

(on sparc-solaris9)
g++ 3.4.3:
- warning of "a" might be used uninitialized, only if -O -Wall
- prints some number

sun studio 9:
- no warnings
- dump core with seg fault

comeau online compiler:
- warning: "a" is used before value is set.

cheers,
/lib

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com (Ron Natalie)
Date: Thu, 25 Nov 2004 04:46:19 GMT
Raw View
bill clarke wrote:
> (this may be an FAQ; i couldn't find anything, but if it is, please
> point it out)
>
> assuming there is no "i" in scope, should the following code be
> accepted?  is it simply undefined behaviour?
>
> """
> int &i = i;
> """
>

"A reference shall be initialized to refer to a valid object..."

Here i is initialized with it's own uninitialized self.   The program
is ill-formed.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: invalid@bigfoot.com (Bob Hairgrove)
Date: Thu, 25 Nov 2004 04:46:31 GMT
Raw View
On Tue, 23 Nov 2004 23:40:16 CST, "bill clarke" <llib@computer.org>
wrote:

>(this may be an FAQ; i couldn't find anything, but if it is, please
>point it out)
>
>assuming there is no "i" in scope, should the following code be
>accepted?  is it simply undefined behaviour?
>
>"""
>int &i = i;
>"""

Section 8.3.2, par. 4 states "[...] A reference shall be initialized
to refer to a valid object or function." Since i is not a valid object
or function, the code is ill-formed.

--
Bob Hairgrove
NoSpamPlease@Home.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]