1

我有一些代码循环遍历一组值并使用每个值调用一个函数。

像耶...

for (int i=0; i<limit; i++) {booleanReturn |= doFunc(i);}

只是好奇,没有编译器会短路 |= 正确(不执行 doFunc(i) 因为 booleanReturn 已经是 TRUE?)

我猜测由于这是一个按位运算符短路操作不是一个定义的行为,但我希望得到一些验证。

4

1 回答 1

3

Operator |= is the compound of operator | not operator ||. The former does not short-circuit. And no operator ||= exists. So the answer to your question is negative. func must be called if it has side effects. If it does not have side effects, then according to the as-if rule the implementation is free to do anything as long as you can't tell the difference.

于 2013-08-21T10:17:41.073 回答