C++ &= 和 |= 运算符是否用于 bool 短路?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23107162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:18:52  来源:igfitidea点击:

Do the &= and |= operators for bool short-circuit?

c++booleanlazy-evaluationshort-circuitingcompound-assignment

提问by iFreilicht

When writing code like this in C++:

在 C++ 中编写这样的代码时:

bool allTrue = true;
allTrue = allTrue && check_foo();
allTrue = allTrue && check_bar();

check_bar()will not be evaluated if check_foo()returned false. This is called short-circuiting or short-circuit evaluationand is part of the lazy evaluation principle.

check_bar()如果check_foo()返回将不会被评估false。这称为短路或短路评估,是惰性评估原则的一部分。

Does this work with the compound assignment operator &=?

这是否适用于复合赋值运算符&=

bool allTrue = true;
allTrue &= check_foo();
allTrue &= check_bar(); //what now?

For logical ORreplace all &with |and truewith false.

对于逻辑OR替换所有&|truefalse

回答by paxdiablo

From C++11 5.17 Assignment and compound assignment operators:

从 C++11 5.17 Assignment and compound assignment operators

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.

E1 op = E2 形式的表达式的行为等价于 E1 = E1 op E2,只是 E1 只计算一次。

However, you're mixing up logical AND which doesshort-circuit, and the bitwise AND which never does.

但是,您正在混淆短路的逻辑 AND和从不短路的按位 AND。

The text snippet &&=, which would be how you would do what you're asking about, is nowhereto be found in the standard. The reason for that is that it doesn't actually exist: there is no logical-and-assignment operator.

文本片段&&=,这将是你会怎么做你问什么,是无处在标准中被发现。原因是它实际上并不存在:没有逻辑和赋值运算符。

回答by Wojtek Surowka

The short-circuit (i.e. lazy) evaluation is only for logical &&and ||. Bitwise &and |evaluate both arguments.

短路(即懒惰)评估仅适用于逻辑&&||。按位&|计算两个参数。

回答by quetzalcoatl

No, they do not cut-short.

不,他们不会缩短。

Note that the &=and |=operators are formed as &+=and |+=. Bit operators&and |does not perform shortcut evaluation.

请注意,&=|=运算符形成为&+=|+ =位运算符&并且|不执行快捷评估。

Only boolean operators&&and ||perform it.

只有布尔运算符&&||执行它。

It means, that a shortcutting operator would have to be traditionally named &&=and ||=. Some languages provide them. C/C++ does not.

这意味着,快捷操作符传统上必须命名为&&=and ||=。一些语言提供了它们。C/C++ 没有。

回答by quetzalcoatl

The code allTrue &= check_foo();is equivalent to allTrue = allTrue & check_foo()In which you are using bitwise ANDand no lazy evaluation is performed.

该代码allTrue &= check_foo();等效于allTrue = allTrue & check_foo()您正在使用的 Inbitwise AND并且不执行延迟评估。

The bitwise ANDmust take two arguments who's binary representation has the same length, and useslogical ANDoperation to compare each corresponding pair of bits.

bitwise AND必须采取两个参数是谁的二进制表示具有相同的长度,并使用logical AND操作比较每个相应的对位。

回答by Danvil

First: a &= b;is not the same as a = a && b;. a &= b;means a = a & b;. In C/C++ there is no a &&= b;.

第一:a &= b;a = a && b;. a &= b;是指a = a & b;。在 C/C++ 中没有a &&= b;.

Logical AND a && bis bit like a test for 1 bit. If the first "bit" is already 0, than the result will always be 0 no matter the second. So it is not necessary to evaluate bif the result is already clear from a. The C/C++ standard allows this optimization.

逻辑与a && b有点像对 1 位的测试。如果第一个“位”已经为 0,那么无论第二个“位”如何,结果都将始终为 0。所以没有必要评估b结果是否已经从a. C/C++ 标准允许这种优化。

Bitwise AND a & bperforms this test for all bits of aand b. So bneeds to be evaluated if at least one bit in awould be non-zero. You could perhaps wish that if a==0, than bwould not be evaluated, but this optimization is not allowed in C/C++.

按位 ANDa & ba和 的所有位执行此测试b。所以b需要评估是否至少有一位a是非零的。您可能希望 if a==0, thanb不会被计算,但是在 C/C++ 中不允许这种优化。

回答by Dr. Debasish Jana

Since & is a bit operation, check_foo() will be evaluated first irrespective of value of allTrue in

由于 & 是一个位操作,check_foo() 将首先被评估,而不管 allTrue 的值如何

allTrue &= check_foo(); // also for allTrue = allTrue & check_foo();

and also

并且

allTrue &= check_bar(); // also for allTrue = allTrue & check_bar();

However, check_foo() will not be called if you use && and alltrue is false as in:

但是,如果您使用 && 并且 alltrue 为 false ,则不会调用 check_foo() ,如下所示:

allTrue = allTrue && check_foo();