简体   繁体   English

运算符| =表示C ++中的布尔值

[英]Operator |= for a boolean in C++

I stumbled upon the following construction in C++: 我在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 . 我认为,这|=是为OR操作的快捷方式,而result将等于true如果这些调用的至少一个最终TryAndDoSomething已经回到true

But now I am wondering if more than one call can actually return true . 但现在我想知道是否有多个电话实际上可以返回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 . 然后只有在返回计算为false调用该方法,也就是说,如果在返回true之前没有其他调用。 Thus after one call returning true , no other call will be done. 因此,在一个呼叫返回true ,将不会进行任何其他呼叫。

Is this the correct interpretation? 这是正确的解释吗?

It's bitwise OR assignment, not short-circuited OR evaluation. 它是按位OR分配,而不是短路OR评估。

It is equivalent to: 它相当于:

result = result | TryAndDoSomething(i);

Not: 不:

result = result || TryAndDoSomething(i);

On booleans, | 关于布尔值, | yields the same result as || 产生与||相同的结果 , but doesn't short-circuit. ,但不会短路。 The right operand of |= is always evaluated. 始终评估|=的右操作数。

The only difference in this context between x |= f() (bitwise OR) and x = x || f() 这个上下文中唯一的区别是x |= f() (按位OR)和x = x || f() x = x || f() (logical OR) is that the latter is short-circuiting. x = x || f() (逻辑或)是后者是短路的。 In the former, f() will be executed n times—unless of course f() throws an exception, but that's another story. 在前者中, f()将被执行n次 - 除非f()当然抛出异常,但这是另一个故事。

In the || || version, f() will no longer be called once x becomes true . 如果x变为true不再调用f() C++ does not have a ||= operator, but it is important to understand that |= and ||= (if it existed) would have different semantics because of this. C ++没有||=运算符,但重要的是要理解|=||= (如果它存在的话)会因此而具有不同的语义。 |= 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 that true and false convert to the integers 1 and 0 , respectively. 作为旁注,如果使用bool ,则按位操作安全的,因为标准指定truefalse转换为整数10 So the only difference in this case is eager versus lazy evaluation. 因此,在这种情况下唯一的区别是渴望和懒惰的评估。

result |= Try() is short for result = result | Try(); result |= Try()result = result | Try();缩写 result = 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 performed a=a||b on each bit of the operands, and doesnt have the quick-bailout thing that logical and/or have. 它具有与在操作数的每个上执行a=a||b相同的效果,并且没有逻辑和/或具有的快速救助事物。 (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). 其他按位运算是& (按位和:每个位上的a=a&&b ),和^ (每个位上的按位xor: a=(a!=b) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM