gpt4 book ai didi

c++ - 未指定时,C++ 运算符的操作数的值类别是什么?

转载 作者:IT老高 更新时间:2023-10-28 22:30:14 26 4
gpt4 key购买 nike

前提:

C++11 标准将表达式分为三个不相交的值类别:lvaluesxvaluesprvalues(第 3.10/1 节)。例如 here 提供了对哪些值类别的说明。 .

我正在努力弄清楚不同运算符对其操作数的值类别的要求是什么。第 3.10/1 段规定:

[...] Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category. [ Note: The discussion of each built-in operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it expects. For example, the built-in assignment operators expect that the left operand is an lvalue and that the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and the categories of values they expect and yield are determined by their parameter and return types. —end note ]

尽管上面的注释声称,第 5 条并不总是很清楚运算符操作数的值类别。例如,这就是关于 assignment 运算符的操作数的值类别的所有内容(第 5.17/1 段):

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. The result in all cases is a bit-field if the left operand is a bit-field. In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single compound assignment operator. —end note ]

正确的操作数怎么样?

“rvalue”和“lvalue”这两个词在整个 5.17 节中不再出现。虽然第 3.10/1 段中的注释明确指出内置赋值运算符期望纯右值作为右操作数,但在第 5.17 节中并未明确提及。即使是 5.17/1 的最后一个注释,它提到了左值到右值的转换,似乎也暗示了右值是预期的(否则需要什么转换?),但注释毕竟是非规范的。

关于其他运算符的部分,包括乘法和加法运算符,通常对其操作数的值类别保持沉默。我在标准中找不到任何“默认语句”指出,如果未另行指定,内置运算符的操作数是右值。因此,问题。

问题:

  1. 赋值运算符右操作数的值类别是什么;而且,更一般地说
  2. 不指定时如何判断运算符操作数的值类别?是否不受约束(意味着接受任何值类别)?如果是这样,为什么左值到右值的转换应该应用于赋值表达式?

高度赞赏对 C++11 标准的引用。

最佳答案

是的,它没有指定,has been covered before .基本上,每次需要左值表达式时都会枚举,因此我们假设每个其他操作数都必须是纯右值表达式。

所以回答你的问题:

  1. prvalue。
  2. 如果未指定,则为纯右值。

链接答案中引用的注释似乎已经更改了几次。 C++11 标准第 3.10 节的引用如下(目前在最新草案中是相同的):

[ Note: The discussion of each built-in operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it expects. For example, the built-in assignment operators expect that the left operand is an lvalue and that the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and the categories of values they expect and yield are determined by their parameter and return types. — end note ]

这里甚至明确表示赋值运算符期望正确的操作数是纯右值。当然,这是一个注释,因此是非规范性的。

关于c++ - 未指定时,C++ 运算符的操作数的值类别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14991219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com