tags:

views:

95

answers:

3

What does an r-value reference look like from a lower-level perspective. I just can't seem to wrap my head around it! Can I see an example of generated code (either equivalent C or x86/x64) from a r-value reference vs. a l-value reference?

For example, what would this construct look like? Let's assume no copy elision for now.

vector<SomethingHUUGE> myFunc();
void foo(vector<SomethingHUUGE>&&);

int main() { foo(myFunc()); return 0; }
+3  A: 

There is no difference for the purposes of code generation. The only semantic difference between the two is that you know an RValue reference is about to be destroyed, while an lvalue reference will not.

Billy ONeal
So r-value references won't generate faster code?
Clark Gaebel
@wowus: Not directly, no. The reason they generate faster code in practice is that they enable move constructors, which can save considerable time. Why copy a vector when you know the source is being destroyed as it goes out of scope?
Billy ONeal
Sorta a different question... but are classes destructed after the move constructor is called, similar to how copy construction is handled?
Clark Gaebel
@wowus: I don't know much about the changes in the C++0x standard. I believe the answer is yes, however. (I could be 110% wrong though... hopefully someone else knows better)
Billy ONeal
The destructor will be called when the appropriate time comes, same as any other object. Whether or not the value was passed to a move constructor has no bearing on that.
Dennis Zickefoose
A: 

An rvalue, in an expression don't have a name.

Most of the time it's a temporary object. If you dont have any way to use it after the expression, then it's an r-value.

Klaim
+1  A: 

it might help to think of both types of references as high-level concepts. Neither of them have an obvious low-level implementation. They can both be represented by pointers, but often, they are simply high-level aliases, and so they have no low-level representation at all. No extra code is generated, the reference is just replaced by the object it references.

In cases where the reference can't be eliminated entirely before code generation, both rvalue and lvalue references are typically represented like pointers, as a memory address.

jalf