Is Short-Circuiting Logical Operators Mandated? and Evaluation Order

Is short-circuiting logical operators mandated? And evaluation order?

Yes, short-circuiting and evaluation order are required for operators || and && in both C and C++ standards.

C++ standard says (there should be an equivalent clause in the C standard):

1.9.18

In the evaluation of the following expressions

a && b
a || b
a ? b : c
a , b

using the built-in meaning of the operators in these expressions, there is a sequence point after the evaluation of the first expression (12).

In C++ there is an extra trap: short-circuiting does NOT apply to types that overload operators || and &&.

Footnote 12: The operators indicated in this paragraph are the built-in operators, as described in clause 5. When one of these operators is overloaded (clause 13) in a valid context, thus designating a user-defined operator function, the expression designates a function invocation, and the operands form an argument list, without an implied sequence point between them.

It is usually not recommended to overload these operators in C++ unless you have a very specific requirement. You can do it, but it may break expected behaviour in other people's code, especially if these operators are used indirectly via instantiating templates with the type overloading these operators.

Question about short circuiting with python logical operators

and has a higher precedence than or, so your code is equivalent to this:

d = (False and False) or True
print(d)

e = True or (False and False)
print(e)

For it to work like you expect, you have to change d to this:

d = False and (False or True)

How does C++ handle &&? (Short-circuit evaluation) [duplicate]

Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2.

"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.

The same happens with the || operator, if bool1 evaluates to true then the whole expression will evaluate to true, without evaluating bool2.

In case you want to evaluate all expressions anyway you can use the & and | operators.

Is Short Circuit Evaluation guaranteed In C++ as it is in Java?

Yes, it is guaranteed for the "built in" types. However, if you overload && or || for your own types, short-circuited evaluation is NOT performed. For this reason, overloading these operators is considered to be a bad thing.

Has c++ standard specified the evaluation order of an operator&&(built-in)? [duplicate]

Yes, it's guaranteed for built-in logical AND operator and logical OR operator by the standard.

(emphasis mine)

[expr.log.and]/1

The && operator groups left-to-right. The operands are both contextually converted to bool. The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

[expr.log.or]/1

The || operator groups left-to-right. The operands are both contextually converted to bool. The result is true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

Unexpected short circuit evaluation when used more than one logical operators in C

This has to do with operator precedence.

The logical AND operator has higher precedence than the logical OR operator ||. So this:

1 || 0 && 0

Parses as:

1 || (0 && 0)

And this:

0 && 0 || 1

Parses as:

(0 && 0) || 1

So in the latter case, first 0 && 0 is evaluated. This results in the value 0, so now you have 0 || 1. The left side of the OR is false so this causes the right side to be evaluated, causing the || operator to result in 1.

Swift short-circuiting with logical operators not working as expected

Short circuiting means that the next part of the expression is not evaluated only if the result is already clear. If the part before && is true then the result can still be both false and true and the next part has to be evaluated.

The cases are:

1. true && true => true
2. true && false => false
3. false && false => false
4. false && true => false

And after evaluating the left operand we have:

true && ??

which can end either in case 1 or 2, which have different results.

On the other hand, if we had:

false && ??

Then the result would be either case 3 or 4, which are both false and the expression will short-circuit.



Related Topics



Leave a reply



Submit