Is Short Circuit Evaluation Guaranteed in C++ as It Is in Java

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.

Why does short-circuit evaluation work when operator precedence says it shouldn't?

Or am I confusing matters?

You are. I think it's much simpler to think about precedence as grouping than ordering. It affects the order of evaluation, but only because it changes the grouping.

I don't know about Javascript for sure, but in Java operands are always evaluated in left-to-right order. The fact that == has higher precedence than || just means that

true || foo == getValue()

is evaluated as

true || (foo == getValue())

rather than

(true || foo) == getValue()

If you just think about precedence in that way, and then consider that evaluation is always left-to-right (so the left operand of || is always evaluated before the right operand, for example) then everything's simple - and getValue() is never evaluated due to short-circuiting.

To remove short-circuiting from the equation, consider this example instead:

A + B * C

... where A, B and C could just be variables, or they could be other expressions such as method calls. In Java, this is guaranteed to be evaluated as:

  • Evaluate A (and remember it for later)
  • Evaluate B
  • Evaluate C
  • Multiply the results of evaluating B and C
  • Add the result of evaluating A with the result of the multiplication

Note how even though * has higher precedence than +, A is still evaluated before either B or C. If you want to think of precedence in terms of ordering, note how the multiplication still happens before the addition - but it still fulfills the left-to-right evaluation order too.

Can I rely on short-circuit evaluation to check vector bounds in C++?

Yes, that works, but it would be more idiomatic to say !vector.empty() && vector[0] == 3: That will work for all containers with maximal efficiency, so it's never worse, sometimes better and always more readable.

Java logical operator short-circuiting

The && and || operators "short-circuit", meaning they don't evaluate the right-hand side if it isn't necessary.

The & and | operators, when used as logical operators, always evaluate both sides.

There is only one case of short-circuiting for each operator, and they are:

  • false && ... - it is not necessary to know what the right-hand side is because the result can only be false regardless of the value there
  • true || ... - it is not necessary to know what the right-hand side is because the result can only be true regardless of the value there

Let's compare the behaviour in a simple example:

public boolean longerThan(String input, int length) {
return input != null && input.length() > length;
}

public boolean longerThan(String input, int length) {
return input != null & input.length() > length;
}

The 2nd version uses the non-short-circuiting operator & and will throw a NullPointerException if input is null, but the 1st version will return false without an exception.

Does C use short circuit evaluation even when arguments are function calls?

Yes, the functions are not called if root1->data == root2->data is false.

Simple check is to do this:

#include <unistd.h>
#include <stdlib.h>

int main(void)
{
write(1, "z", 1);
if ((1 == 0) && write(1, "a", 1) && write(1, "b", 1))
{
write(1, "c", 1);
}
write(1, "d", 1);
return (EXIT_SUCCESS);
}

Short circuiting statement evaluation -- is this guaranteed? [C#]

Yes, this is guaranteed.

C# Language Specification - 7.11 Conditional logical operators:

The && and || operators are called the conditional logical operators. They are also called the "short-circuiting" logical operators.

Therefore they will support logical short-circuiting by definition - you can rely on this behavior.

Now it is important to make a distinction between a conditional operator and a logical operator:

  • Only conditional operators support short-circuiting, logical operators do not.
  • C#'s logical operators look just like their conditional counterparts but with one less character so a logical OR is | and a logical AND is &.
  • Logical operators can be overloaded but conditional operators cannot (this is a bit of an technicality as conditional operator evaluation does involve overload resolution and this overload resolution can resolve to a custom overload of the type's logical operator, so you can work around this limitation to a certain extent).

Is there a way to disable short circuit evaluation in Java?

There is no compiler or JVM option for changing the semantics of boolean expression evaluation.

If you cannot modify the source, possible (albeit not guaranteed) options include:

  • Creatively recreate the conditions you seek to test via elaborate
    setup of preconditions.
  • Use mock objects.
  • Hack the compiler.
  • Hack the JVM.
  • Twiddle the byte code.

Sorry, those are all much more difficult than a compiler/JVM option or modifying the source. Further, the last three options (as well as the requested compiler/JVM option or modifying the source) violate proper testing protocol of not modifying what's being tested.

Clarification on code regarding compilers Order of Evaluation

I'm not sure that I understand what you are expecting as a response to the question.

But it's easy to characterise the difference between short-circuit evaluation and strict order of evaluation.

Consider the statement:

b = f(a) + g(a)

In a language which guarantees strict left-to-right evaluation order (such as Java), then function f will be called before the function g, but *both functions will always be called.

On the other hand, with the expression

b = f(a) || g(a)

f will be called first, but it is quite possible that g will not be called. The evaluation can stop after f returns, if it returns a true value.

There are also languages, like C, that don't guarantee left-to-right evaluation. In those languages, when f(a) + g(a) is evaluated, it is possible that g is called before f. But short-circuit evaluation (with boolean operators) works the same because short-circuit evaluation must be strictly ordered.



Related Topics



Leave a reply



Submit