CPP & Value Category

Attention:博主的翻译水平是战五渣,尽量读原文。前几日有未知网友对我进行评论,大意就是讽刺我水平差(本来就差),求我别翻译。其实我想说一句:我接受建议但是不接受意见,如果您看着不爽,请出门左转,不要BB。一般的的文字性错误或者理解错误想提醒我改正的可以直接底下回复提醒我改正。但是如果你只是想在评论去里去显摆下你的水平,对不起不欢迎您,攻击性的评论看见必删。

前言:

为什么要翻译这篇文章呢?答案是官网提供的中文版简直就是不能看,所以当了搬运工。我感觉这篇文章挺好的,虽然讲的不是那么易懂,有些地方有点晦涩。但是作为参考文档还是可行的。翻译了几段,因为英语不是很好,有些地方可能表达的不到位甚至是错误,希望大神告诉我,我好改进。现在就开始后悔当初没好好学习英语,很多能看懂的东西却不会表达,所以翻译相对比较渣。有能力的还是建议看英文,以免对您形成误导。
原文链接

Value categories(值类别)

Each C++ expression (an operator with its arguments, 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.


每个C++ 表达式(带参数的操作,字面量,变量名)的特征是有两个独立的属性,一个是类型另一个是值。每个表达式有一些非引用类型并且必定属于三种基础值类型之一。

Primary categories

1.lvalue

An lvalue is an expression that identifies a non-temporary object or a non-member function.

左值是这样的一个被标识为非临时对象或者非成员函数的表达式。

The following expressions are lvalues:
The name of a variable or function in scope, regardless of type, such as std::cin or std::endl. Even if the variable’s type is rvalue reference, the expression consisting of its name is an lvalue expression.

作用域中的变量名,函数。例如cin等。甚至变量的类型是右值引用类型只要它具名那么就是左值。

Function call or overloaded operator expression if the function’s or overloaded operator’s return type is an lvalue reference, such as std::getline(std::cin, str) or std::cout << 1 or str1 = str2 or ++iter

函数调用或者某些重载操作,只要他们的的返回类型是左值引用类型,那么也是左值。

Built-in pre-increment and pre-decrement, dereference, assignment and compound assignment, subscript (except on an array xvalue), member access (except for non-static non-reference members of xvalues, member enumerators, and non-static member functions), member access through pointer to data member if the left-hand operand is lvalue, comma operator if the right-hand operand is lvalue, ternary conditional if the second and third operands are lvalues.

大意是内置的前置递增递减操作符作用的对象得到的结果也是左值。

Cast expression to lvalue reference type.

转换成左值引用类型的表达式也是左值。

String literal

String 字面值。

Function call expression if the function’s return type is rvalue reference to function type (rare)

如果函数的返回类型是右值引用类型,那么函数调用表达式可以是一个左值。比较少见。
Cast expression to rvalue reference to function.
转换成右值引用类型的函数表达式。

(since C++11)

Properties:(属性)
Same as glvalue (below)

同glvae一致。
Address of an lvalue may be taken: &++i[1] and &std::endl are valid expressions.
可以被取址。
A modifiable lvalue may be used as the left-hand operand of the built-in assignment operator.
可修改的左值可以被当作内置赋值操作符的左操作数(对象)
An lvalue may be used to initialize an lvalue reference; this associates a new name with the object identified by the expression.
左值可以用来初始化一个左值引用,相当于给左值起了一个别名。

rvalue (until C++11) / prvalue (since C++11)

A prvalue** (“pure” rvalue)** is an expression that identifies a temporary object (or a subobject thereof) or is a value not associated with any object.

纯右值是这样的一个表达式:被视为临时对象或者是在其中的子对象抑或是没有同任何对象关联的值。

The following expressions are prvalues:

下面表达式都是纯右值:

Literal (except string literal), such as 42 or true or nullptr.

字面值,例如42,true,nullptr.

Function call or overloaded operator expression if the function’s or the overloaded operator’s return type is not a reference, such as str.substr(1, 2) or str1 + str2

函数调用的返回类型不是引用类型,例如str.substr(1,2)或者str1+str2.不清楚substr函数的可以去查一下函数原型。

Built-in post-increment and post-decrement , arithmetic and logical operators, comparison operators, address-of operator, member access for a member enumerator, a non-static member function, or a non-static non-reference data member of an rvalue, member access through pointer to a data member of rvalue or to a non-static member function, comma operator where the right-hand operand is rvalue, ternary conditional where either second or third operands aren’t lvalues.

内置的后置自增自减操作符,算术运算符,逻辑运算符等操作符作用下得到的结果是纯右值。

Cast expression to any type other than reference type.

把表达式转换成引用类型之外的其他任何类型。

Lambda expressions, such as [](int x){return x*x;}

Lambda表达式也是纯右值,不清楚的可以参考前面的博文。

(since C++11)
Properties:
Same as rvalue (below)

同右值一致。

a prvalue cannot be polymorphic: the dynamic type of the object it identifies is always the type of the expression.

纯右值不可能是多态的。它被认为的动态类型总是表达式的类型

a non-class prvalue cannot be cv-qualified.

非类 纯右值不可能是局部cv-qualified 的。cv表示const/volatile ,.

a prvalue cannot have incomplete type (except for type void, see below, or when used in decltype specifier)

纯右值不可能是不完全类型。除了下文介绍的void类型。

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.

xvalue 值是这样的一种类型:被视为将要消失的对象,也就是说这个对象将要被移走。这个对象可能是匿名临时对象,也可能是在某作用域中的具名对象或者其他类型的对象。但是当xvalue被用作一个函数的参数时,xvlaue总是被绑定到右值引用类型,前提是存在这样的一个函数并且形参是右值引用类型。

Only the following expressions are xvalues:(只有下面的表达式是xvalues).

A function call or overloaded operator expression if the function’s or the overloaded operator’s return type is an rvalue reference to object type, such as std::move(val)
A cast expression to an rvalue reference to object type, such as static_cast

Mixed categories(混合类别)

glvalue

A glvalue (“generalized” lvalue) is an expression that is either an lvalue or an xvalue.

左值或者xvalue类型之一就是gvalue.
Properties (note: these apply to pre-C++11 lvalues as well)
A glvalue may be implicitly converted to prvalue with lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit conversion.
A glvalue may be polymorphic: the dynamic type of the object it identifies is not necessarily the static type of the expression.
A glvalue can have incomplete type, where permitted by the expression

rvalue

An rvalue is an expression that is either a prvalue or an xvalue.
Properties (note, these apply to both xvalues and prvalues, which means they apply to the pre-C++11 rvalues as well)
Address of an rvalue may not be taken: &int(), &i++[2], &42, and &std::move(val) are invalid.
Rvalues can’t be used as the left-hand operand of the built-in assignment or compound assignment operator.
An rvalue may be used to initialize a const lvalue reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.
An rvalue may be used to initialize an rvalue reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.
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 move constructor, and likewise with copy and move assignment operators).
(since C++11)

Special categories(特殊类别)

Pending member function call

The expressions obj.func and ptr->func, where func is a non-static member function, and 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 expressions

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 some contexts (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 may be used as the second and the third operands of the conditional operator ?: (other void prvalues can only be used if appearing as both 2nd and 3rd operands).

Bit fields

An expression that designates a bit field (e.g. s.x where s is an object of type struct S { int x:3; };) is an lvalue expression (or xvalue if s is one): it may be used on the left hand side of the assignment operator, but its address cannot be taken and a non-const lvalue reference cannot be bound to it. A const lvalue reference can be initialized from a bit-field lvalue, but a temporary copy of the bit-field will be made: it won’t bind to the bit field directly.

footnotes

↑ Assuming i has built-in type or the pre-increment operator is overloaded to return by lvalue reference
↑ Assuming i has built-in type or the postincrement operator is not overloaded to return by lvalue reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值