复合赋值 E1 op= E2 不等同于 E1 = E1 op E2
Compound assignment E1 op= E2 is not equivalent to E1 = E1 op E2
cppreference 说:
the behavior of every builtin compound-assignment expression E1 op= E2
(where E1
is a modifiable lvalue expression and E2
is an rvalue expression or a braced-init-list (since C++11)) is exactly the same as the behavior of the expression E1 = E1 op E2
, except that the expression E1
is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. in f(a += b, g())
, the +=
is either not started at all or is completed as seen from inside g()
)."
我想知道这个解释是错误的(不充分的)还是我理解有误。
我知道 E1 = E1 + E2
和 E1 += E2
之间存在内在差异,
这是解释 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
具有以下行为:
- 对
E1
和E2
进行求值(顺序不确定),将两次求值之间的运算结果赋值给E1
,即(E1) = (E1) op (E2)
.
那么对复合赋值操作行为的更好解释是
(E1) = (E1) op (E2)
? (或 E1 = E1 op (E2)
,因为 E1
只能有优先级高于赋值运算符和优先级低于 op 运算符的运算符,没有括号,如果 E1 op (E2)
想产生与 [=30= 不同的结果]. 这种结果是可修改左值的运算符不存在。)
这是关于运算符优先级的。 +
的优先级高于三元条件(以及 ==
)。所以这个:
x = x + (-1) ? 2 : 2;
是这样评价的:
x = (x + (-1)) ? 2 : 2;
这就是为什么您得到 2
而不是 3
的原因。与另一个样本相同:
x = x + (-2) == 0
被评估为
x = (x + (-2)) == 0 // (x + (-2)) is 0, 0 == 0 is 1
以第一个例子为例:
x = x + (-1) ? 2 : 2;
这里A
就是x
,B
就是(x + (-1) ? 2 : 2)
(x
加上三元运算的结果)
然而这里
x += (-1) ? 2 : 2;
A
是x
,B
只是三元运算的结果。由于 +=
的优先级低于 =
,A
和 B
发生了变化。所以关于 E1 = E1 + E2 和 E1 += E2 相同的说法是正确的,但这并不意味着将 E1 = E1 + E2
转换为 E1 += E2
永远不会改变 E1
和 E2
的意思。这似乎有点误导。
根据 C++ 标准(8.5.18 赋值和复合赋值运算符)
7 The behavior of an expression of the form E1 op = E2 is equivalent
to E1 = E1 op E2 except that E1 is evaluated only once. In += 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.
和(8.5.6 加法运算符)
1 The additive operators + and - group left-to-right. The usual
arithmetic conversions (8.3) are performed for operands of arithmetic
or enumeration type.
这些表达式语句
x = x + (-1) ? 2 : 2;
x += (-2) == 0;
x = x + (-2) == 0;
相当于
x = ( x - 1 ) ? 2 : 2;
x += ( -2 == 0 );
x = ( ( x - 2 ) == 0 );
cppreference 的引用直接来自 C++ 标准:
[expr.ass]/6
The behavior of an expression of the form E1 op= E2
is equivalent to E1 = E1 op E2
except that E1
is evaluated only once.
Such expressions are deprecated if E1
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.
在这种情况下,the term expression has already been defined 和 E1 op= E2
等同于 E1 = E1 op E2
显然并不意味着表达式在它们的文本表示中是等价的,但在它们的解析(类型,值和副作用)。
[A] better explanation for compound assignment operation's behavior could be (E1) = (E1) op (E2)?
我只能表达我的意见:我认为 cpprederence 页面在这里引用标准是正确的,但可以添加注释以确保读者不会理解错.
cppreference 说:
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()
)."
我想知道这个解释是错误的(不充分的)还是我理解有误。
我知道 E1 = E1 + E2
和 E1 += E2
之间存在内在差异,
这是解释 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
具有以下行为:
- 对
E1
和E2
进行求值(顺序不确定),将两次求值之间的运算结果赋值给E1
,即(E1) = (E1) op (E2)
.
那么对复合赋值操作行为的更好解释是
(E1) = (E1) op (E2)
? (或 E1 = E1 op (E2)
,因为 E1
只能有优先级高于赋值运算符和优先级低于 op 运算符的运算符,没有括号,如果 E1 op (E2)
想产生与 [=30= 不同的结果]. 这种结果是可修改左值的运算符不存在。)
这是关于运算符优先级的。 +
的优先级高于三元条件(以及 ==
)。所以这个:
x = x + (-1) ? 2 : 2;
是这样评价的:
x = (x + (-1)) ? 2 : 2;
这就是为什么您得到 2
而不是 3
的原因。与另一个样本相同:
x = x + (-2) == 0
被评估为
x = (x + (-2)) == 0 // (x + (-2)) is 0, 0 == 0 is 1
以第一个例子为例:
x = x + (-1) ? 2 : 2;
这里A
就是x
,B
就是(x + (-1) ? 2 : 2)
(x
加上三元运算的结果)
然而这里
x += (-1) ? 2 : 2;
A
是x
,B
只是三元运算的结果。由于 +=
的优先级低于 =
,A
和 B
发生了变化。所以关于 E1 = E1 + E2 和 E1 += E2 相同的说法是正确的,但这并不意味着将 E1 = E1 + E2
转换为 E1 += E2
永远不会改变 E1
和 E2
的意思。这似乎有点误导。
根据 C++ 标准(8.5.18 赋值和复合赋值运算符)
7 The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once. In += 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.
和(8.5.6 加法运算符)
1 The additive operators + and - group left-to-right. The usual arithmetic conversions (8.3) are performed for operands of arithmetic or enumeration type.
这些表达式语句
x = x + (-1) ? 2 : 2;
x += (-2) == 0;
x = x + (-2) == 0;
相当于
x = ( x - 1 ) ? 2 : 2;
x += ( -2 == 0 );
x = ( ( x - 2 ) == 0 );
cppreference 的引用直接来自 C++ 标准:
[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.
在这种情况下,the term expression has already been defined 和 E1 op= E2
等同于 E1 = E1 op E2
显然并不意味着表达式在它们的文本表示中是等价的,但在它们的解析(类型,值和副作用)。
[A] better explanation for compound assignment operation's behavior could be (E1) = (E1) op (E2)?
我只能表达我的意见:我认为 cpprederence 页面在这里引用标准是正确的,但可以添加注释以确保读者不会理解错.