Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?

匆匆过客 提交于 2019-12-05 19:57:46

Yes, it is performed. Basically, it's all because 3.0 > 1.2 is a well-formed expression, that contains nothing but prvalues for operands.

First, [expr]/9 states (emphasis mine) that

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

So the question really boils down to "Do the relational operators expect prvalues for operands"? And the answer to that is also yes. For we need to consider [expr.rel]/1:

relational-expression:
  shift-expression
  relational-expression < shift-expression
  relational-expression > shift-expression
  relational-expression <= shift-expression
  relational-expression >= shift-expression

The operands shall have arithmetic, enumeration, or pointer type. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

The above grammar production is the important bit. We can follow it (I won't do it entirely here) and reduce shift-expression to a primary-expression. And one of the productions of a primary-expression is a literal. For which it is said in [expr.prim.literal]:

A literal is a primary expression. Its type depends on its form. A string literal is an lvalue; all other literals are prvalues.

And because most literals are prvalues, I think it's safe to say the relational operators expect prvalues for operands.

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