lvalue vs rvalue vs xvalue vs glvalue vs prvalue

value categories
  • lvalue (locator value): An lvalue (so-called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. Lvalues can appear on both sides of an assignment operator. Examples:
    • Variables: int x = 10;
    • Dereferenced pointers: *p = 20;
    • Array elements: arr[0] = 30;
  • xvalue (eXpiring value): An xvalue (an “eXpiring” value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references.
    Examples:
    • The result of calling a function whose return tyep is an rvalue reference is an xvalue.
  • glvalue (generalized lvalue): A glvalue is an lvalue or an xvalue.
  • rvalue (right-hand value): 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. Examples:
    • Literal values: 10, "hello".
    • Expressions that result in temporary values: x + y, std::move(obj).
  • prvalue (pure rvalue): A prvalue is a type of rvalue that is not an xvalue. It is a temporary value that cannot be accessed or modified. Examples:
    • The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true, is also a prvalue.

In summary:
lvalue: An expression that refers to an object or function with a name and memory location.
rvalue: An expression that is not an lvalue, representing a temporary value.
xvalue: A special type of rvalue that is an expired lvalue whose resources can be reused.
glvalue: A collective term that includes both lvalues and xvalues.
prvalue: A type of rvalue that is not an xvalue, representing a pure temporary value.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。