运算符| =在C一个布尔++ [英] Operator |= for a boolean in C++
问题描述
我偶然发现在C ++下面的结构:
I stumbled upon the following construction in C++:
bool result = false;
for(int i = 0; i<n; i++){
result |= TryAndDoSomething(i);
}
我认为,这 | =
是OR运算符的快捷方式,而结果
将等于真正
最后,如果这些调用 TryAndDoSomething
已返回真正$ C中的至少一个$ C>。
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);
那么只有当收益评估,以<code>假,也就是说,如果没有其他调用返回之前真正$ C $的方法将被调用C>。经过这样一个调用返回
真正
,没有其他的通话将被完成。
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.
这是正确的跨pretation?
Is this the correct interpretation?
推荐答案
在布尔值, |
产生相同的结果为 ||
,但不会短路。右边的操作数 | =
始终被评估
On booleans, |
yields the same result as ||
, but doesn't short-circuit. The right operand of |=
is always evaluated.
这篇关于运算符| =在C一个布尔++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!