Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remember the differences between rvalue, lvalue, etc?

Tags:

c++

mnemonics

This question is great because it has answers on what are the differences between rvalue, lvalue, xvalue, glvalue, and prvalue. The top answer has a decent explanation of each, and I understand their differences when I read them. However, I'm having a hard time keeping them straight in my head, and I have to google them every time someone uses these terms to remind myself which one is which.

Are there any common mnemonics or other ways people remember which one is which? I'd really like to be able to keep them all straight in my head without mixing them up as I currently do.

like image 476
Cornstalks Avatar asked Dec 18 '12 18:12

Cornstalks


People also ask

What is the difference between an lvalue and an rvalue?

An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it.

What is the difference between L value and R value references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.

What are Rvalues?

An rvalue (so-called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. A prvalue (“pure” rvalue) is an rvalue that is not an xvalue.

What does R value mean C++?

An rvalue is an expression that is not an lvalue. Examples of rvalues include literals, the results of most operators, and function calls that return nonreferences. An rvalue does not necessarily have any storage associated with it.


1 Answers

I make 2 associations:

  • you can only take the address of an lvalue
  • xvalues is what an std::move expression is

If it's neither then it's probably a prvalue.

Now, to remember glvalues and rvalues I simply imagine this graph:

lvalues and xvalues are both glvalues, while xvalues and prvalues are both rvalues. That's really all you need to know in practice.

Maybe a mnemonic to remember lvaluse, xvalues, and prvalues go at the bottom are the words Lower eXPRessions (it contains L, X, and PR). Maybe not.

like image 95
Pubby Avatar answered Oct 07 '22 00:10

Pubby