Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When writing code like this in 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 evaluation and is part of the lazy evaluation principle.

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.

like image 973
iFreilicht Avatar asked Apr 16 '14 10:04

iFreilicht


People also ask

What is a doe animal?

Definition of doe (Entry 1 of 2) : the adult female of various mammals (such as a deer, rabbit, or kangaroo) of which the male is called buck.

What is a Douce?

Definition of douce chiefly Scotland. : sober, sedate the douce faces of the mourners— L. J. A. Bell.

What is do the honor?

Definition of do the honors : to do the actions performed by a host or hostess My mother cooks a big turkey for Thanksgiving every year, and when it comes to carving, my father does the honors at the table. The Ambassador did the honors by introducing the guest speaker.

Is there a word do?

1 : to cause (as an act or action) to happen : perform Tell me what to do.


1 Answers

From 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.

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

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

like image 87
paxdiablo Avatar answered Sep 23 '22 17:09

paxdiablo