C++ 中布尔值的运算符 |=
我偶然发现了 C++ 中的以下构造:
bool result = false;
for(int i = 0; i<n; i++){
result |= TryAndDoSomething(i);
}
我认为此 |=
是 OR 运算符的快捷方式,并且 result
将等于 true
最后,如果至少其中一个对 TryAndDoSomething
的调用返回了 true
。
但现在我想知道是否多个调用实际上可以返回 true
。事实上,如果我们将操作扩展为:
result = result || TryAndDoSomething(i);
那么仅当 return 计算为 false
时才会调用该方法,也就是说,如果之前没有其他调用返回 true
。因此,在一次调用返回 true
后,将不再执行其他调用。
这是正确的解释吗?
I stumbled upon the following construction in C++:
bool result = false;
for(int i = 0; i<n; i++){
result |= TryAndDoSomething(i);
}
I supposed that this |=
was a shortcut for the OR operator, and that result
would equal true
in the end if at least one of these calls to TryAndDoSomething
had returned true
.
But now I am wondering if more than one call can actually return true
. Indeed if we extend the operation as:
result = result || TryAndDoSomething(i);
Then the method will be called only if return evaluated to false
, that is, if no other call before returned true
. Thus after one call returning true
, no other call will be done.
Is this the correct interpretation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是按位或赋值,而不是短路或评估。
相当于:
不:
It's bitwise OR assignment, not short-circuited OR evaluation.
It is equivalent to:
Not:
对于布尔值,
|
产生与||
相同的结果,但不会短路。|=
的右操作数始终被求值。On booleans,
|
yields the same result as||
, but doesn't short-circuit. The right operand of|=
is always evaluated.在这种情况下,
x |= f()
(按位或)和x = x || 之间的唯一区别f()
(逻辑或)是后者短路了。在前者中,f()
将被执行n
次——当然,除非f()
抛出异常,但那是另一回事了。在
||
版本中,一旦x
变为true
,将不再调用f()
。 C++ 没有||=
运算符,但重要的是要了解|=
和||=
(如果存在)因此具有不同的语义。|=
不仅仅是缺失的||=
的替代品。附带说明一下,如果您使用的是
bool
,按位运算是安全的,因为标准指定true
和false< /code> 分别转换为整数
1
和0
。因此,这种情况下唯一的区别是急切求值与惰性求值。The only difference in this context between
x |= f()
(bitwise OR) andx = x || f()
(logical OR) is that the latter is short-circuiting. In the former,f()
will be executedn
times—unless of coursef()
throws an exception, but that’s another story.In the
||
version,f()
will no longer be called oncex
becomestrue
. C++ does not have a||=
operator, but it is important to understand that|=
and||=
(if it existed) would have different semantics because of this.|=
is not just a replacement for the missing||=
.As a side note, provided you are using
bool
, the bitwise operation is safe, because the standard specifies thattrue
andfalse
convert to the integers1
and0
, respectively. So the only difference in this case is eager versus lazy evaluation.result |= Try()
是result = result | 的缩写。尝试();
。||
运算符您似乎理解,但|
运算符却有很大不同。它的名称是按位或(与逻辑或相对)。它具有与对操作数的每个位执行a=a||b
相同的效果,并且没有逻辑和/或具有的快速救助功能。 (它的速度也疯狂;与加法一样快或更快)。其他按位运算是&
(按位与:每个位上的a=a&&b
)和^
(按位异或:每个位上的a=(a!=b)
)。result |= Try()
is short forresult = result | Try();
. The||
operator you seem to understand, but the|
operator is quite different. It's name is bitwise or (as opposed to logical or). It has the same affect as if it performeda=a||b
on each bit of the operands, and doesnt have the quick-bailout thing that logical and/or have. (It's also crazy fast; as fast or faster than addition). The other bitwise operations are&
(bitwise and:a=a&&b
on each bit), and^
(bitwise xor:a=(a!=b)
on each bit).