C++新标准—— Value categories
Each C++ expression (an operator with its operands, a literal, a variable name, etc.) is characterized by two independent properties: a type and a value category. Each expression has some non-reference type, and each expression belongs to exactly one of the three primary value categories: prvalue, xvalue, and lvalue.
每个C++表达式(带有操作数的运算符、字面量、变量名等)都具有两个独立特性:类型 和 值类别。每个表达式都拥有某种非引用类型 (),并且每个表达式必定属于三大基本值类别之一:纯右值(prvalue)、将亡值(xvalue)和左值(lvalue)。
- a glvalue (“generalized” lvalue) is an expression whose evaluation determines the identity of an object or function; 【译】泛左值是一种表达式,其求值结果可确定对象或函数的身份;【注】所谓 “确定身份” 是指有名字、地址标识;
- a prvalue (“pure” rvalue) is an expression whose evaluation
- computes the value of an operand of a built-in operator (such prvalue has no result object), or
- initializes an object (such prvalue is said to have a result object).【译】纯右值是一种表达式,其求值过程要么计算内置运算符操作数的值(此类纯右值没有结果对象),要么用于初始化对象(此类纯右值被称为具有结果对象)。结果对象可以是变量、通过new表达式创建的对象、通过临时物化创建的临时对象,或是这些对象的成员。请注意,非void类型的被丢弃表达式也拥有结果对象(即物化生成的临时对象)。此外,所有类和数组类型的纯右值都拥有结果对象,除非它们作为decltype的操作数
- an xvalue (an “eXpiring” value) is a glvalue that denotes an object whose resources can be reused;【译】xvalue(“将亡值”,即 Expiring value)是一种表示其资源可被重用的对象的泛左值。【注】它代表一个对象,这个对象所持有的资源(比如动态分配的内存、文件句柄等)可以被“窃取”并用于别处,而不是被销毁。
- an lvalue is a glvalue that is not an xvalue; lvalue(左值)是一种不属于xvalue(将亡值)的glvalue(泛左值)
一、lvalue The following expressions are lvalue expressions:
- the name of a variable, a function, a template parameter object(since C++20), or a data member, regardless of type, such as std::cin or std::hex. Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression (but see Move-eligible expressions);【译】变量、函数、模板形参对象(自 C++20 起)或数据成员的名称(无论其类型如何),例如 std::cin 或 std::endl,均构成左值表达式。即使变量的类型是右值引用,由其名称构成的表达式仍然是左值表达式
- a function call or an overloaded operator expression, whose return type is lvalue reference, such as std::getline(std::cin, str), std::cout << 1, str1 = str2, or ++it;【译】函数调用或重载运算符表达式,若其返回类型为左值引用,则构成左值表达式。例如:std::getline(std::cin, str)、std::cout << 1、str1 = str2 或 ++it
- a = b, a += b, a %= b, and all other built-in assignment and compound assignment expressions;【译】a = b, a += b, a %= b 以及所有其他内置赋值表达式和复合赋值表达式。
- ++a and --a, the built-in pre-increment and pre-decrement expressions; 【译】内置的前置递增和前置递减表达式。
- *p, the built-in indirection expression;【译】内置的解引用表达式。
- a[n] and p[n], the built-in subscript expressions, where one operand in a[n] is an array lvalue(since C++11); 【译】a[n] 和 p[n](内置的下标表达式),其中 a[n] 的一个操作数是数组左值(自 C++11 起)。
- a.m, the member of object expression, except where m is a member enumerator or a non-static member function, or where a is an rvalue and m is a non-static data member of object type;【译文】对象成员访问表达式,除非 m 是成员枚举项或非静态成员函数,或者 a 是右值且 m 是对象类型的非静态数据成员。
- p->m, the built-in member of pointer expression, except where m is a member enumerator or a non-static member function; 【译】(内置的指针成员访问表达式),除非 m 是成员枚举项或非静态成员函数。
- a.*mp, the pointer to member of object expression, where a is an lvalue and mp is a pointer to data member;【译】对象成员指针访问表达式,其中 a 为左值且 mp 为指向数据成员的指针。
- p->*mp, the built-in pointer to member of pointer expression, where mp is a pointer to data member;【译】内置的指针成员访问表达式,其中 mp 为指向数据成员的指针。
- a, b, the built-in comma expression, where b is an lvalue; 【译】内置的逗号表达式,其中 b 为左值。
- a ? b : c, the ternary conditional expression for certain b and c (e.g., when both are lvalues of the same type, but see definition for detail);【译】特定 b 和 c 的三元条件表达式(例如,当两者为同类型的左值时,具体细节参见定义)。
- a string literal, such as "Hello, world!";【译文】字符串字面量
- a cast expression to lvalue reference type, such as static_cast<int&>(x) or static_cast<void(&)(int)>(x);【译】转换为左值引用类型的强制转换表达式,例如 static_cast<int&>(x) 或 static_cast<void(&)(int)>(x)。
- a constant template parameter of an lvalue reference type; 【译】左值引用类型的常量模板形参
- a function call or an overloaded operator expression, whose return type is rvalue reference to function; 【译】函数调用或重载运算符表达式,其返回类型为函数的右值引用。
- a cast expression to rvalue reference to function type, such as static_cast<void(&&)(int)>(x).【译】转换为函数类型右值引用的强制转换表达式,例如 static_cast<void(&&)(int)>(x)。
二、prvalue The following expressions are prvalue expressions:
-
a literal (except for string literal), such as 42, true or nullptr;
-
a function call or an overloaded operator expression, whose return type is non-reference, such as str.substr(1, 2), str1 + str2, or it++;【译】函数调用或重载运算符表达式,其返回类型为非引用类型,例如 str.substr(1, 2)、str1 + str2 或 it++。
-
a++ and a--, the built-in post-increment and post-decrement expressions;【译】内置的后置递增和后置递减表达式。
-
a + b, a % b, a & b, a << b, and all other built-in arithmetic expressions;【译】a + b, a % b, a & b, a << b 以及所有其他内置算术表达式。
-
a && b, a || b, !a, the built-in logical expressions;【译】内置的逻辑表达式
-
a < b, a == b, a >= b, and all other built-in comparison expressions;【译】a < b, a == b, a >= b 以及所有其他内置比较表达式。
-
&a, the built-in address-of expression;【译】内置的取址表达式
-
a.m, the member of object expression, where m is a member enumerator or a non-static member function;【译】对象成员访问表达式,其中 m 是成员枚举项或非静态成员函数。
-
p->m, the built-in member of pointer expression, where m is a member enumerator or a non-static member function;【译】(内置的指针成员访问表达式),其中 m 是成员枚举项或非静态成员函数。
-
a.*mp, the pointer to member of object expression, where mp is a pointer to member function;【译】对象成员指针访问表达式,其中 mp 为指向成员函数的指针。
-
p->*mp, the built-in pointer to member of pointer expression, where mp is a pointer to member function;【译】内置的指针成员访问表达式,其中 mp 为指向成员函数的指针。
-
a, b, the built-in comma expression, where b is an prvalue;【译】内置的逗号表达式,其中 b 为纯右值
-
a ? b : c, the ternary conditional expression for certain b and c (see definition for detail);【译】特定 b 和 c 的三元条件表达式。
-
a cast expression to non-reference type, such as static_cast(x), std::string{}, or (int)42;【译】转换为非引用类型的强制转换表达式,例如 static_cast(x)、std::string{} 或 (int)42。
-
the this pointer;
-
an enumerator;
-
a constant template parameter of a scalar type;
-
a lambda expression
三、xvalue The following expressions are xvalue expressions:
-
a.m, the member of object expression, where a is an rvalue and m is a non-static data member of an object type;【译】对象成员访问表达式,其中 a 为右值且 m 为对象类型的非静态数据成员。
-
a.*mp, the pointer to member of object expression, where a is an rvalue and mp is a pointer to data member;【译】对象成员指针访问表达式,其中 a 为右值且 mp 为指向数据成员的指针。
-
a, b, the built-in comma expression, where b is an xvalue;【译】a, b(内置的逗号表达式),其中 b 为将亡值。
-
a ? b : c, the ternary conditional expression for certain b and c (see definition for detail); 【译】(三元条件表达式),适用于特定的 b 和 c 组合。
-
a function call or an overloaded operator expression, whose return type is rvalue reference to object, such as std::move(x);【译文】函数调用或重载运算符表达式,其返回类型为对象的右值引用,例如 std::move(x)。
-
a[n], the built-in subscript expression, where one operand is an array rvalue;【译】内置的下标表达式,且其中一个操作数为数组右值
-
a cast expression to rvalue reference to object type, such as static_cast<char&&>(x);【译】转换为对象类型右值引用的强制转换表达式,例如 static_cast<char&&>(x)。
更多推荐

所有评论(0)