开发者

What is the difference between r-value references and l-value references? (CodeGen)

开发者 https://www.devze.com 2023-01-04 07:31 出处:网络
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-

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, wh开发者_Go百科at 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; }


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消