Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initialising reference to itself

2 views
Skip to first unread message

bill clarke

unread,
Nov 24, 2004, 12:40:16 AM11/24/04
to
(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...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Ron Natalie

unread,
Nov 24, 2004, 11:46:19 PM11/24/04
to
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.

Bob Hairgrove

unread,
Nov 24, 2004, 11:46:31 PM11/24/04
to
On Tue, 23 Nov 2004 23:40:16 CST, "bill clarke" <ll...@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
NoSpam...@Home.com

Michiel Salters

unread,
Nov 27, 2004, 6:53:03 PM11/27/04
to
r...@sensor.com (Ron Natalie) wrote in message news:<41a48364$0$1604$9a6e...@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

0 new messages