2

Is it okay for me to presume in C or C++ or JavaScript or any other modern language that if I do…

bool funt1(void) {…}
bool funt2(void) {…}
if (funt1() && funt2()) {Some code}

… Am I guaranteed that both functions get called or if funt1 returns false can the compiler bail on me and never call funt2?

David G
  • 94,763
  • 41
  • 167
  • 253
  • 3
    in most languages - if funt1 returns false, second will NOT be called – Iłya Bursov Jan 27 '15 at 21:26
  • 7
    [Short-circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation) – quantdev Jan 27 '15 at 21:26
  • 1
    Why no short-circuit in C++ for overloaded operators: http://stackoverflow.com/questions/25913237/is-there-actually-a-reason-why-overloaded-and-dont-short-circuit – Deduplicator Jan 27 '15 at 22:21
  • http://stackoverflow.com/questions/1232603/do-all-programming-languages-have-boolean-short-circuit-evaluation wiki suggests it's up to the compiler as to whether or not it short circuits for fortran. – Tom Tanner Jan 28 '15 at 09:03
  • Many thanks for the clarification. I consider my question answered. Do I close the question or does the moderator? I'm not really familiar with all the details of how this site works. – Chris Young Jan 28 '15 at 19:21

3 Answers3

4

In C, C++, and Javascript, the logical operators && and || short circuit, i.e. in A && B, A is evaluated first and B evaluated if and only if A returned true. Similarly, in A || B, B is evaluated if and only if A returned false. These are guaranteed by the language standards and apply even if B has side effects (in fact, this can be used to control those side effects).

In C++, these rules only apply to the logical operators applied to built in types, but not to user-defined logical operators of the same name. However, in your code snippet, two bool are compared, so this cannot be a user-defined &&.

Walter
  • 44,150
  • 20
  • 113
  • 196
  • Now we just need an example of a "modern" language where short-circuiting is not used... – Deduplicator Jan 27 '15 at 21:52
  • 2
    @Deduplicator According to the Wikipedia site linked in quantdev's comment, [MUMPS](http://en.wikipedia.org/wiki/MUMPS) does not have short circuit, but I don't know nothing about this illness (including whether it supports operators `AND` and/or `OR`). However, pretty much any computer language has short circuit evaluation on these two operators. – Walter Jan 27 '15 at 21:56
  • The only thing I know about it (hearsay) validates that characterization. But here's a tip: C++ operator overloading. – Deduplicator Jan 27 '15 at 22:01
  • @Deduplicator Okay, user-defined `operator&&` and `operator||` (on other than two built-in types) will not use short circuit evaluation. But that is not really in the realm of the OP. – Walter Jan 27 '15 at 22:18
3

There is a pitfall in C++: If the first and the second function return objects and both are needed for the logical operation, there is no short circuit.

#include <iostream>

struct A {};
struct B {};

bool operator || (const A&, const B&) { return true; }

A a() { std::cout << "A\n"; return A(); }
B b() { std::cout << "B\n"; return B(); }

bool f() { std::cout << "f\n"; return true; }
bool g() { std::cout << "g\n"; return true; }

int main(int argc, char* argv[])
{
    if(a() || b());
    if(f() || g());
}
  • Actually, when the operator is overloaded. One of them not having any impact on the final result does not change anything. Shameless plug of a question I answered about the feasability of allowing overloaded operators to short-circuit: https://stackoverflow.com/a/25913422 – Deduplicator Jan 27 '15 at 22:10
1

You asked:

Am I guaranteed that both functions get called or if funt1 returns false can the compiler bail on me and never call funt2?

If funt1() returns false, it is guaranteed that funt2() will never get called.

If funt1() returns true, it is guaranteed that funt2() will get called.

R Sahu
  • 204,454
  • 14
  • 159
  • 270