Value categories
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
当一个C + +表达式(操作符与它的参数)进行评估,其结果是一些非引用类型的值。每个表达式的主要价值类别的一个.
原文:
When a C++ expression (an operator with its arguments) is evaluated, it results in a value of some non-reference type. Each expression belongs to exactly one of the primary value categories.
目录 |
[编辑] 主要类别
[编辑] 左值
“左值”是一个表达式,用于标识一个非临时对象或一个非成员函数.
原文:
An lvalue is an expression that identifies a non-temporary object or a non-member function.
下面的表达式是左值
原文:
The following expressions are lvalues:
- 范围内的对象或函数的名称,无论类型,如
std::cin
或std::endl
。即使对象的类型是右值引用,表达它的名字是一个左值表达式.原文:The name of an object or function in scope, regardless of type, such asstd::cin
orstd::endl
. Even if the object's type is rvalue reference, the expression consisting of its name is an lvalue expression. - 函数调用操作符表达式的功能或操作者的返回类型是一个左值引用,如
std::getline(cin, str)
或cout << 1
或或
++n
或*p
原文:Function call or operator expression if the function's or operator's return type is an lvalue reference, such asstd::getline(cin, str)
orcout << 1
oror
++n
or*p
- 强制转换表达式的左值的引用类型.原文:Cast expression to lvalue reference type.
- 如果函数的返回类型的函数调用表达式的右值引用功能类型(罕见)原文:Function call expression if the function's return type is rvalue reference to function type (rare)
- CAST表达式的右值引用的功能.原文:Cast expression to rvalue reference to function.
属性
- 同glvalue(如下图)
- 一个左值的地址可以采取:
&++i
和&std::endl
是有效的表达式.原文:Address of an lvalue may be taken:&++i
and&std::endl
are valid expressions. - 修改的左值,可以使用内置的赋值运算符的第一个参数(左).原文:A modifiable lvalue may be used as the first (left) argument of the built-in assignment operator.
[编辑] 右值NJ/ prvalueNJ
A“prvalue”是一个表达式,用于标识一个临时对象(或子对象除外),或不与任何对象关联的值.
原文:
A prvalue is an expression that identifies a temporary object (or a subobject thereof) or is a value not associated with any object.
下面的表达式prvalues
原文:
The following expressions are prvalues:
- 文字,如42或true或nullptr.原文:Literal, such as 42 or true or nullptr.
- 如果函数调用/操作符表达式的功能或操作人员的返回类型是不是一个引用,如
str.substr(1, 2)
或2+2
原文:Function call/operator expression if the function's or the operator's return type is not a reference, such asstr.substr(1, 2)
or2+2
- CAST表达式引用类型以外的其他任何类型.原文:Cast expression to any type other than reference type.
- Lambda表达式,如 [](int x){return x*x;}原文:Lambda expressions, such as [](int x){return x*x;}
属性
- 同右值(如下图)
- 一个prvalue不能是多态对象的动态类型,它确定类型的表达式.原文:a prvalue cannot be polymorphic: the dynamic type of the object it identifies is always the type of the expression.
- 不能被const限定非类prvalue的.原文:a non-class prvalue cannot be const-qualified.
[编辑] xvalueNJ
一个 xvalue“,用于标识一个”届满“对象的,即,从可移动的对象,该对象是一个表达式。可能是一个无名的临时标识的对象由一个xvalue的表达,它可能是一个命名对象,范围,或任何其他类型的对象,但如果作为函数的参数,xvalue将始终绑定到右值引用过载,如果有的话.
原文:
An xvalue is an expression that identifies an "expiring" object, that is, the object that may be moved from. The object identified by an xvalue expression may be a nameless temporary, it may be a named object in scope, or any other kind of object, but if used as a function argument, xvalue will always bind to the rvalue reference overload if available.
只有以下的表达式是xvalues:
原文:
Only the following expressions are xvalues:
- 一个函数调用表达式,如果函数的返回类型是一个右值引用的对象类型,如
std::move(val)
原文:A function call expression, if the function's return type is an rvalue reference to object type, such asstd::move(val)
- 一个右值引用的对象类型强制转换表达式,如
static_cast<T&&>(val)
或(T&&)val
原文:A cast expression to an rvalue reference to object type, such asstatic_cast<T&&>(val)
or(T&&)val
- 类的非静态成员访问表达式,其中的对象表达式的值为xValue原文:a non-static class member access expression, in which the object expression is an xvalue
- 在第一个操作数的指针成员表达是xValue和第二个操作数是一个指向数据成员的指针.原文:A pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.
属性
- 同右值(如下图)
- 同glvalue(如下图)
像prvalues,xvalues绑定到右值引用,,但不像prvalues,值为xValue,可能是多态的,非类值为xValue可能是合格的CV-.
原文:
Like prvalues, xvalues bind to rvalue references, but unlike prvalues, an xvalue may be polymorphic, and a non-class xvalue may be cv-qualified.
[编辑] 混合类
[编辑] glvalueNJ
一个glvalue是一个表达式,它是一个左值,或一个xvalue的.
原文:
A glvalue is an expression that is either an lvalue or an xvalue.
属性
- 一个glvalue可能是多态的动态类型的对象,它标识不一定是静态类型的表达式.原文:A glvalue may be polymorphic: the dynamic type of the object it identifies is not necessarily the static type of the expression.
[编辑] 右值NJ
右边的值可以是一个表达式,它可以是一个prvalue或值为xValue.
原文:
An rvalue is an expression that is either a prvalue or an xvalue.
属性(注意,这些适用于都的xvalues和prvalues,这意味着它们适用于前C + +11的右值以及)
原文:
Properties (note, these apply to both xvalues and prvalues, which means they apply to the pre-C++11 rvalues as well)
- 一个rvalue的地址可能不会采取
&i++
和&42
和&std::move(val)
是无效的.....原文:Address of an rvalue may not be taken:&i++
and&42
and&std::move(val)
are invalid. - 可以使用一个rvalue初始化一个常量左值引用,在这种情况下,由右值标识的对象的寿命延长,直到参考端的范围.原文:An rvalue may be used to 初始化一个常量左值引用, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.
- 当作为函数的参数时,两个重载的函数的情况下,一个右值引用参数和其他左值const参数,右值绑定到右值引用过载(因此,如果复制和移动的构造是,右值参数调用移动的构造函数,同样的复制和移动赋值运算符).原文:When used as a function argument and when two overloads of the function are available, one taking rvalue reference parameter and the other taking lvalue reference to const parameter, rvalues bind to the rvalue reference overload (thus, if both copy and move constructors are available, rvalue arguments invoke the 移动的构造函数, and likewise with copy and move assignment operators).
[编辑] 特殊类别
[编辑] 的成员函数调用之前
表达obj.*mfp和ptr->*mfp地方
mfp
是一个指向成员函数的指针,分类prvalue的表达,但他们可以不被使用,以初始化引用,作为函数的参数,或为任何目的,在所有,除非在左侧的参数的函数,调用表达式,例如: (pobj->*ptr)(args).原文:
The expressions obj.*mfp and ptr->*mfp where
mfp
is a pointer to member function, are classified as prvalue expressions, but they cannot be used to initialize references, as function arguments, or for any purpose at all, except as the left-hand argument of a function call expression, e.g. (pobj->*ptr)(args).[编辑] 无效的表达
函数调用表达式返回void,投的表达式来void,和扔表达式被列为prvalue的表达,但它们不能被用来初始化引用或作为函数的参数。它们可以被用来在一个废弃价值的情况下(例如,在一个属于自己的行,左边的参数的逗号操作符等),并在return声明一个函数返回void。抛出表达式(但并非其他的空隙prvalues)此外,也可以使用作为第二个和第三个操作数的条件运算符?.
原文:
Function call expressions returning void, cast expressions to void, and throw-expressions are classified as prvalue expressions, but they cannot be used to initialize references or as function arguments. They can be used in a discarded-value context (e.g. on a line of its own, as the left argument of the comma operator, etc) and in the return statement in a function returning void. In addition, throw-expressions (but not other void prvalues) may be used as the second and the third operands of the 条件运算符?.