Topic: 4.1 Lvalue-to-rvalue conversion


Author: gb@127.0.0.1 ("Gene Bushuyev")
Date: Mon, 16 May 2005 22:04:18 GMT
Raw View
Does the section "4.1 Lvalue-to-rvalue conversion" of the standard imply
that the following code has an undefined behavior?

int i; // uninitialized
int& j = i; // undefined behavior?

If the standard indeed doesn't allow taking reference to an unitialized
variable, was it a deliberate decision? Is there something about compilers
that may prevent them from safely doing that?

Quote:
4.1/1
An lvalue (3.10) of a non-function, non-array type T can be converted to an
rvalue. If T is an incomplete type, a program that necessitates this
conversion is ill-formed. If the object to which the lvalue refers is not an
object of type T and is not an object of a type derived from T, or if the
object is uninitialized, a program that necessitates this conversion has
undefined behavior.


Thanks,

Gene Bushuyev

---
[ 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: "Me" <anti_spam_email2003@yahoo.com>
Date: Mon, 16 May 2005 19:59:46 CST
Raw View
No, 3.10/8 talks about reference l/rvalues and links to 8.5.3 which is
the appropriate section. 8.5.3/5 specifically deals with your example.

---
[ 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: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Wed, 18 May 2005 00:13:16 GMT
Raw View
Gene Bushuyev wrote:
> Does the section "4.1 Lvalue-to-rvalue conversion" of the standard imply
> that the following code has an undefined behavior?
>
> int i; // uninitialized
> int& j = i; // undefined behavior?
>
> If the standard indeed doesn't allow taking reference to an unitialized
> variable, was it a deliberate decision? Is there something about compilers
> that may prevent them from safely doing that?
>

4.1 does not apply in this case. This is a case of "direct binding"
described in 8.5.3/5. A note in the text explicitly reports that "the
usual lvalue-to-rvalue (4.1), array-to-pointer (4.2), and
function-to-pointer (4.3) standard conversions are not needed, and
therefore are suppressed, when such direct bindings to lvalues are done."

Alberto

---
[ 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                       ]