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 ]
"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.
>(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
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