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
Do the &= and |= operators for bool short-circuit?
提问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 OR
replace all &
with |
and true
with false
.
对于逻辑OR
替换所有&
与|
和true
带false
。
回答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 AND
and no lazy evaluation is performed.
该代码allTrue &= check_foo();
等效于allTrue = allTrue & check_foo()
您正在使用的 Inbitwise AND
并且不执行延迟评估。
The bitwise AND
must take two arguments who's binary representation has the same length, and useslogical AND
operation 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 && b
is 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 b
if 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 & b
performs this test for all bits of a
and b
. So b
needs to be evaluated if at least one bit in a
would be non-zero. You could perhaps wish that if a==0
, than b
would not be evaluated, but this optimization is not allowed in C/C++.
按位 ANDa & b
对a
和 的所有位执行此测试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();