James Kanze
11/18/2008 10:57:00 PM
On Nov 18, 6:53 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> James Kanze wrote:
[...]
> >> The compiler warns about it because the code takes the
> >> address of a temporary, although there's nothing inherently
> >> wrong with doing that.
> > Has this changed in the latest draft. According to my copy of
> > the standard (version 1998---out of date, I know), "The
> > operand [of the unary & operator] shall be an lvalue or a
> > qualified-id". His expression was &Test("test2"); IMHO, the
> > compiler generated a warning because it was being laxist.
> > [..]
> There is an inherent controversy around this. Example:
> struct Foo {
> Foo(int) {}
> Foo& that() { return *this; }
> };
> int main() {
> Foo(42); // expression that yields an rvalue
> Foo(42).that(); // expression that is an lvalue
> }
> How would you explain that? :-)
I wouldn't try to:-). But the language standard defines a
concept of lvalue and rvalue, or at least insists that they
exist, that certain expressions return lvalues, others rvalues,
and that certain operators require lvalues, others rvalues (and
that there is an lvalue to rvalue conversion---so that in
something like i=j, where both i and j have type int, there is
an implicit conversion involved). The language says that the
expression Test("test1") is an rvalue, and it says that unary &
requires an lvalue, and so be it.
> You can't avoid being "laxist" with the language that contains
> such pearls.
Such "pearls" are probably inevitable as soon as you try to
maintain the distinction between lvalues and rvalues, and also
have real objects. (At one point, I actually suggested that we
drop the concept of lvalue/rvalue completely, and simply state
that all temporaries were considered const. This would have
allowed things like &3, with type int const, but after all, we
allow this indirectly already, if you bind 3 to a const
reference, and then take its address.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34