C++的左值与右值
Expressions are categorized according to the taxonomy in Figure 1.
# Figure1
# expression
# / \
# glvalue rvalue
# / \ / \
# lvalue xvalue prvalue
-
An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue. — end example ]
-
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 (8.3.2). [ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. — end example ]
-
A glvalue (“generalized” lvalue) is an lvalue or an xvalue.
-
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) 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. [ Example: 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. — end example ]
Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category .
每个表达式都完全属于此分类法中的基本分类之一:lvalue、xvalue 或 prvalue。 表达式的此属性称为其值类别
For example, the built-in assignment operators expect that the left operand is an lvalue and that the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and the categories of values they expect and yield are determined by their parameter and return types .
例如,内置赋值运算符期望左操作数是左值,右操作数是右值,并产生左值作为结果。 用户定义的运算符是函数,它们期望和产生的值的类别由它们的参数和返回类型决定。
Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue。
每当泛左值出现在需要纯右值的上下文中时,泛左值就会转换为纯右值
reference:
[1]. http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3797.pdf
本文详细介绍了C++编程语言中的左值(lvalue)、右值(rvalue)以及它们的子类别xvalue和prvalue的概念。左值可以出现在赋值表达式的左侧,而右值通常出现在右侧。glvalue是lvalue和xvalue的统称。文章还阐述了表达式的值类别,以及在不同上下文中如何转换。理解这些概念对于深入理解C++的底层工作原理至关重要。

2万+

被折叠的 条评论
为什么被折叠?



