复合分配E1 op = E2不等于E1 = E1 op E2 [英] Compound assignment E1 op= E2 is not equivalent to E1 = E1 op E2
问题描述
cppreference 说:
每个内置复合赋值表达式
E1 op= E2
的行为(其中E1
是可修改的左值表达式,而E2
是右值表达式或大括号初始列表(自C ++ 11起))是完全正确的与表达式E1 = E1 op E2
的行为相同,不同之处在于表达式E1
仅被评估一次,并且对于不确定顺序的函数调用(例如,在f(a += b, g())
中,+=
从g()
内部看完全没有开始或已完成."
the behavior of every builtin compound-assignment expression
E1 op= E2
(whereE1
is a modifiable lvalue expression andE2
is an rvalue expression or a braced-init-list (since C++11)) is exactly the same as the behavior of the expressionE1 = E1 op E2
, except that the expressionE1
is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. inf(a += b, g())
, the+=
is either not started at all or is completed as seen from insideg()
)."
我想知道这个解释是错误的(不足)还是我理解不正确.
I want to know if this explanation is wrong (insufficient) or I'm understanding something incorrectly.
我了解E1 = E1 + E2
和E1 += E2
之间存在内在的区别,
在此处进行了解释:
I understand there's inherent difference between E1 = E1 + E2
and E1 += E2
,
which is explained here:
#include<iostream>
int main() {
int x;
x = 1;
x += (-1) ? 2 : 2;
std::cout << x << std::endl; //prints 3
x = 1;
x = x + (-1) ? 2 : 2;
std::cout << x << std::endl; //prints 2
x = 2;
x += (-2) == 0;
std::cout << x << std::endl; //prints 2
x = 2;
x = x + (-2) == 0; // prints 1
}
我的猜测是E1 op= E2
具有以下行为:
My guess is E1 op= E2
has the following behavior:
- 评估
E1
和E2
(不确定顺序),并将两次评估之间的运算结果分配给E1
,即(E1) = (E1) op (E2)
.
- Evaluate
E1
andE2
(unsure about the order) and assign result of operation between two evaluation toE1
, which is(E1) = (E1) op (E2)
.
因此对复合赋值操作的行为的更好解释是
(E1) = (E1) op (E2)
? (或E1 = E1 op (E2)
,因为如果E1 op (E2)
想要产生与(E1) op (E2)
不同的结果,则E1
只能具有比赋值运算符优先级更高的运算符,并且比op运算符具有更低的优先级,并且没有括号.可修改的左值不存在.)
So would a better explanation for compound assignment operation's behavior be
(E1) = (E1) op (E2)
? (or E1 = E1 op (E2)
, because E1
only can have operator of higher precedence than assignment operator and lower precedence than op operator, without parentheses, if E1 op (E2)
wants to produce different result from (E1) op (E2)
. Such operator whose result is a modifiable lvalue doesn't exist.)
推荐答案
cppreference中的引用直接来自C ++标准:
That quote from cppreference comes directly from the C++ Standard:
[expr.ass]/6
E1 op= E2
形式的表达式的行为与E1 = E1 op E2
等效,除了E1
仅被评估一次.
如果E1
具有 volatile限定的类型,则不赞成使用此类表达式;请参阅[depr.volatile.type]
.
对于+=
和-=
,E1
应该具有算术类型,或者是指向可能由cv限定的完全定义的对象类型的指针. 在其他所有情况下,E1
都应具有算术类型.
[expr.ass]/6
The behavior of an expression of the form
E1 op= E2
is equivalent toE1 = E1 op E2
except thatE1
is evaluated only once.
Such expressions are deprecated ifE1
has volatile-qualified type; see[depr.volatile.type]
.
For+=
and-=
,E1
shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object type.
In all other cases,E1
shall have arithmetic type.
在这种情况下,已经定义了术语表达式 和E1 op= E2
等同于E1 = E1 op E2
并不意味着这些表达式在文本表示形式上是等效的,但在它们的分辨率(类型,值和副作用)上是等效的.
In this context, the term expression has already been defined and E1 op= E2
being equivalent to E1 = E1 op E2
clearly doesn't mean the expressions are equivalent in their textual representation, but in their resolution (type, value and side-effects).
[A]对于化合物分配操作的行为的更好解释可能是(E1)=(E1)op(E2)?
[A] better explanation for compound assignment operation's behavior could be (E1) = (E1) op (E2)?
我只能发表自己的看法:我认为 cpprederence页面在这里引用标准是正确的,但是可以添加注释以确保读者不会误解.
I only can express my opinion: I think the cpprederence page is right to quote the Standard here, but a note could be added to make sure readers do not get it wrong.
这篇关于复合分配E1 op = E2不等于E1 = E1 op E2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!