关于左值到右值的转换,何时需要? [英] Regarding lvalue-to-rvalue conversion, when is it required?

查看:211
本文介绍了关于左值到右值的转换,何时需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上阅读了很多,似乎很多人提到以下规则(但我在标准中找不到),

I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard),

加法运算符+(和所有其他二进制运算符)要求两个操作数都为右值,结果为右值。
等等..

The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on..

我检查了C ++标准,并清楚地说明(第3.10 / 2节),

I checked the C++ standard, and it clearly states that (clause 3.10/2),


每当一个glvalue出现在一个预期为prvalue的
上下文中,
glvalue将被转换为prvalue

Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue

(第5/9节),


每当glvalue表达式出现
作为运算符的操作数,
期望该操作数的prvalue,
的左值到值(4.1),
数组到指针(4.2)或
函数到指针(4.3)标准
转换用于将
表达式转换为prvalue。

Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue.


$ b b

它使用一个术语操作数期望一个prvalue。然而,当我看到加法运算符,乘法运算符等,它只提到,结果是一个prvalue,但它没有说什么操作数是预期。

It uses a term the operand "expects" a prvalue. However, when I look into addition operator, multiplication operator etc, it only mentions that, the result is a prvalue, but it doesn't say anything on what the operands are "expected" to be.

二元运算符是否真正期望操作数是prvalue在以下情况下有所不同:

Whether does the binary operator really expects the operands to be prvalue makes a difference in the following case,

int b = 2;
int a = b + 1;

如果b预期为prvalue,那么在这里会有一个左值到右值的转换,然后它将执行prvalue + prvalue并返回一个prvalue,并将结果prvalue分配给一个lvalue a。

If b is expected to be a prvalue, there will be a lvalue-to-rvalue conversion here, and then it will perform prvalue + prvalue and return a prvalue, and the result prvalue is assigned to an lvalue a.

但是,如果b不需要是prvalue ,它将是lvalue + prvalue,结果是prvalue。

However, if b is not required to be a prvalue, it would be lvalue + prvalue and the result is a prvalue.

我真的想知道标准在哪里明确或隐含地提到
规则不同的运营商?我检查所有的操作符部分,只有少数操作符,标准明确地提到操作数和结果是否应该是左值或右值。对于大多数运算符,标准只提到结果,但不提到操作数的要求。

I really want to know where does the standard explicitly or implicitly mentions that the rules for different operators? I check all the operators section and only a few operators that the standards explicitly mentions whether the operands and results shall be lvalue or rvalue. For most operators, the standard only mentions the result but not the operand requirement.

谢谢。

Btw,我发现在标准5.19中关于常量表达式可能非常隐含地意味着二元运算符需要对操作数进行左值到右值转换。有关详情,请参阅我上一个问题,

Btw, I found in Standard 5.19 regarding constant expression may very very "implicitly" imply that the binary operator require lvalue-to-rvalue conversion on operands. For more details, please refer to my previous question,

混合使用constexpr和const?


条件表达式是常数
表达式,除非它涉及
中的一个作为潜在的
评估的子表达式(3.2)。

A conditional-expression is a constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2).

...

- 一个左值到右值的转换(4.1)
,除非它应用于

— an lvalue-to-rvalue conversion (4.1) unless it is applied to

----一个glvalue
积分或枚举类型
指向一个非易失性const对象
具有前面的初始化,
用常量表达式初始化

———— a glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression

感谢您阅读。

推荐答案

和不合格的标准部分;但是,在3.10中

So, this is generally one of those inferred and ill-specified parts of the standard; However, in 3.10



[注意:一些内置操作符期望使用左值操作数。 [示例:内置赋值运算符都期望其左手操作数为左值。 - end example]其他内置运算符产生右值,有些人期望它们。 [示例:一元和二元+运算符期望右值参数和yield rvalue结果。 - end example]第5节中每个内置运算符的讨论指示它是否期望使用左值操作数以及它是否产生左值。 - end note]

[ Note: some built-in operators expect lvalue operands. [ Example: built-in assignment operators all expect their left-hand operands to be lvalues. — end example ] Other built-in operators yield rvalues, and some expect them. [ Example: the unary and binary + operators expect rvalue arguments and yield rvalue results. — end example ] The discussion of each built-in operator in clause 5 indicates whether it expects lvalue operands and whether it yields an lvalue. — end note ]


注意指定语言不良在第5节中表示是否需要lvalue操作数以及它是否产生一个左值。

Notice the poorly specified language "in clause 5 indicates whether it expects lvalue operands and whether it yields an lvalue".

检查第5章指出,列举每个表达式需要或返回左值的情况,列举,我认为其余的假设是右值。

Examination of Chapter 5 indicates that every case where an expression needs or returns an lvalue is enumerated, however very few cases dealing specifically with rvalues are enumerated, I think it's then assumed that the rest is assumed to be rvalues.

我也怀疑它的指定不好,因为从标准的角度来看,它不是特别重要如果转换是由运算符隐式或显式完成的,不管,行为应该是一致的和良好的行为。

I also suspect it's poorly specified because from the perspective of the standard, it's not particularly important if the conversion is done implicitly or explicitly by the operator, regardless, the behavior should be the consistent and well-behaved.

这篇关于关于左值到右值的转换,何时需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆