What is rvalue and lvalue?

I found different definitions on different resourcesrvalue, lvalue. How is it correct?

right value or read value?

left value or locator value?

 12
Author: αλεχολυτ, 2018-08-02

2 answers

Initially, we were talking about the right and left parts relative to the assignment operator. But the correct version of such a simple decryption is not and cannot be. The terms will remain rvalue and lvalue. But what they mean is clearly spelled out in the standard. All this forms categories of expressions.

expression hierarchy

  • Glvalue ("generalized" lvalue)

    An expression whose calculation defines the entity of an object, bit field, or functions.

  • Prvalue ("pure" rvalue)

    An expression whose evaluation initializes an object, a bit field, or evaluates the value of an operator's operand, according to the context of use. For example, literals 42, true, nullptr except for string literals, which are lvalue expressions.

  • Xvalue ("eXpiring" lvalue)

    This is the glvalue, which denotes an object or bit field whose resources can be reused (usually because they are near the end of their lifetime). For example, the result of calling std::move gives the expression xvalue.

  • Lvalue

    This is glvalue, which is not xvalue. For example, the name of a variable, function, or data member, regardless of its type, even a variable that has an rvalue reference type, forms an expression lvalue.

  • Rvalue

    This is prvalue or xvalue.

Thus, any expression is primarily lvalue, xvalue or prvalue. rvalue is already a generalization.

 20
Author: αλεχολυτ, 2020-10-25 18:51:45

I don't know what you mean by "how to do it right". rvalue and lvalue are categories of expressions. Here is what is written in the standard:

- A glvalue is an expression whose evaluation determines the identity of an object, bit-field, or function.
- A prvalue is an expression > whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.
- An xvalue is a glvalue that denotes an object or bit-field whose resources can be reused (usually because it is near the end of its lifetime).
- An lvalue is a glvalue that is not an xvalue.
- An rvalue is a prvalue or an xvalue.

//...
[Note: Historically, lvalues and rvalues were so-called because they could appear on the left- and right-hand side of an assignment (although this is no longer generally true); glvalues are "generalized" lvalues, prvalues are "pure" rvalues, and xvalues are "eXpiring" lvalues. Despite their names, these terms classify expressions, not values. - end note]

 3
Author: Croessmah, 2018-08-02 10:24:11