Re: What's going on here?!

From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 02/01/05


Date: Tue, 01 Feb 2005 14:27:16 +0000

BigMan wrote:

> Thanks, Tom!
> Now I see... and it sound very strange that the standard allows a copy of
> the rvalue to be created.
> So, can anyone tell me why the standard allows a copy of the rvalue to be
> created instead of just binding the reference to the rvalue.

I think it's because of the lifetime extension properties of const
references. e.g.

Foo makeFoo();
Foo const& f = makeFoo(); //temporary lasts as long as f.

The compiler might require the temporary to be copied into a particular
area of the stack in order that it persist until f goes out of scope.

However, because this only potential need only arises when the lifetime
of a temporary is extended, it has been suggested that the standard
change to say that a copy won't be made in the case where the temporary
will only last until the end of the full expression in which it is
created. See:

http://groups-beta.google.com/group/comp.std.c++/msg/6700d973aedf00fc

Tom



Relevant Pages

  • Re: Casting double to int produces inconsistent results from VS 2003 to VS 2008
    ... Some things in the standard are ... 4.8p1 An rvalue of floating point type can be converted to an rvalue of ... another floating point type. ... the result of the conversion is ...
    (microsoft.public.vc.language)
  • Re: copy constructors and printf
    ... Basically they imply that an rvalue ... the copy constructor until the va_arg call correct? ... but I remember seeing somewhere in the Standard (I can't find ... TIA, Dave Moore ...
    (comp.lang.cpp)
  • Re: _Complex_I
    ... What is sometimes called "rvalue" is in this International ... Standard described as the "value of an expression". ... declared as designating an object ... I don't see an explicit statement that the result is the value of the ...
    (comp.std.c)
  • Re: returning lvalue in C vs C++
    ... basis that fis an rvalue, and a method call on an ... rvalue is OK, but an rvalue on the lhs of an assignment is not. ... But I thought that the expression rvalue.x makes x an lvalue, because The Standard didn't say that rvalue.x must be an rvalue. ... However, I have CHANGED MY STANCE on when assignment is allowed: I now think the standard is too unclear for a straight answer such as the one I gave, which, as I see it know, may even be considered /incorrect/. ...
    (comp.programming)