运算符| =在C一个布尔++ [英] Operator |= for a boolean in C++

查看:165
本文介绍了运算符| =在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 已返回真正

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>假,也就是说,如果没有其他调用返回之前真正。经过这样一个调用返回真正,没有其他的通话将被完成。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆