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.
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.
“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.
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.
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.
I make 2 associations:
std::move
expression isIf 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With