Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short-circuiting of non-booleans

Tags:

c++

c

Is it safe to shorten this usage of the ternary operator:

process_ptr(ptr ? ptr : default_ptr);

with the short-circuit:

process_ptr(ptr || default_ptr);

in C and C++? In other words, are we guaranteed to get either ptr or default_ptr back from the experssion, or is it perhaps allowed for the expression to result in an arbitrary "logical true" value, if the expression is logically true?

This is the kind of code you'd see all over Perl code, but I rarely see it in C/C++, that's the original basis of my question.

like image 806
Irfy Avatar asked Jun 03 '15 13:06

Irfy


People also ask

What is boolean short-circuiting?

Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR ) you can stop as soon as you find the first condition which satisfies or negates the expression.

Which operator is a non short-circuiting logical?

The | and & logical operators, known as non-short circuit operators, should not be used. Using a non-short circuit operator reduces the efficiency of the program, is potentially confusing and can even lead to the program crashing if the first operand acts as a safety check for the second.

Does Python short-circuit booleans?

In python, short-circuiting is supported by various boolean operators and functions. The chart given below gives an insight into the short-circuiting case of boolean expressions. Boolean operators are ordered by ascending priority.

Why short-circuit boolean evaluation is useful?

Advantages Of Short-Circuit Evaluation: It can be helpful in avoiding computationally expensive tasks under certain circumstances. It provides a check for the first argument without which the second argument may result in a runtime error.


1 Answers

The second expression will evaluate to either 1 or 0.

Quoting the C11 standard draft:

6.5.14 Logical OR operator

  1. The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

So the two expressions are very different, since one of them yields a pointer, and the other one an integer.

Edit:

One of the comments claims that this answer is only valid for c, and @Lightness Races in Orbit is right.

There are also answers that are only correct for c++1, although the only difference with them is that c++ has type bool and then it evaluates this expression as bool instead of int. But apparently there is an important issue with overloading || operator in c++, which prvents short-citcuiting to apply for the object that overloads it.

So for c++ there are more things to consider, but since this question was tagged with both languages tags, then it's necessary to mention at least the differece.

The rule still applies when short-circuiting applies, i.e. the result of the evaluation of the expressions is either 1 or 0 for c and true or false for c++.


1 Like these answers: 1, 2

like image 72
Iharob Al Asimi Avatar answered Sep 20 '22 13:09

Iharob Al Asimi