Regarding lvalue-to-rvalue conversion, when is it required?

£可爱£侵袭症+ 提交于 2019-11-28 12:18:58

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

[ 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 ]

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

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.

(First of all, sorry for my English. Corrections are absolutely wellcome)

The standard says:

§5.7-3 The result of the binary + operator is the sum of the operands. [...]

Let's suppose we have the expression e1 + e2, and the + operator selected is the built-in one, the expression is well-formed, e1 and e2 are arithmetic types or a casting to arithmetic type is available, and everything is OK, fine and perfect!

So, rule §5.7-3 applies. In other hand, each operand is an expressions:

§5-1 [Note: [...] An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects. — end note]

It says an expression can result in a value because of void expressions, like the delete expression or a voidfunction, but since we have said e1 + e2 is a perfectly-defined expression!, we can omit the verb "can", and so we can afirm that: an expression results in a value.

Last point: for arithmetical and logical built-in operators, I understand, although it isn't specified by the standard, only the value of its operands matter, irrespective of the value category of them.

I think that with being expressions is enough for implementing the built-in operator + (and other arithmetical operators), since only the value matters, and the value can be reached through the expression. For this reason, the Standard doesn't define them clearly.

All the same, this type of things are very bad structured. For example, I've found no place specifing, when an operator receive an object as operand and not a direct value (doubt that I'm currently trying to solve), if the operator should take directly its value for computing the operator, if the value is the result of evaluating the object, and so on. It's obvious that only values matters, but, what exactly says the Standard for these things is a sort of mystery.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!