28

In Java, I use

if (a != null && a.fun());

by taking full advantage of short-circuit evaluation and expression are evaluated from left to right?

In C++, can I do the same? Are they guarantee to portable across different platform and compiler?

if (a != 0 && a->fun());
Lazer
  • 90,700
  • 113
  • 281
  • 364
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • 5
    Try googling a bit yourself, I am sure that with the keywords you already have in the question you would have found the answer. Google for 'c++ short-circuit' and you will probably find many results that directly answer your question. – David Rodríguez - dribeas Jan 21 '10 at 10:42
  • 7
    You could probably say the same thing about 85% of the questions asked here. – John Dibling Jan 21 '10 at 15:34
  • 2
    possible duplicate of [Is short-circuiting boolean operators mandated in C/C++? And evaluation order?](http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order) – jpalecek Jun 09 '10 at 10:29
  • 5
    @dribeas Too bad you weren't prescient enough to realize that by now the #1 google hit for 'c++ short-circuit' is this very page...I'm glad someone put a real answer below! – C S Mar 02 '12 at 20:10
  • 4
    I googled "is c++ short circuited", this page is the first hit... – digit plumber Apr 21 '12 at 20:31
  • This is the first post I find when I google the question. I thought there's a big point of SO posts is to make google search easier, so I wouldn't mind some duplicates if different wording of similar question make people's life easier. – Superziyi Apr 16 '15 at 23:51

2 Answers2

42

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.

  • 1
    This is to my knowledge the exact reason operator overloading is not in Java. – Thorbjørn Ravn Andersen Jan 21 '10 at 11:24
  • @Thorbjørn: could be true, but sounds mad to me - Java could have allowed operator overloading but not permitted overloads of those two operators (or the ternary ?: operator, which C++ doesn't overload either). I always assumed it was because of the "code that looks local should be local" argument, that anything which calls user-defined code should "look like" it calls out of line, with an explicit method call. – Steve Jessop Jan 21 '10 at 15:09
  • 2
    Madness is in the eye of the beholder. Overloading has some very distinct disadvantages, namely that you cannot trust ANYTHING to have the behaviour you expect, is enough for me to think Java got this right. – Thorbjørn Ravn Andersen Jan 21 '10 at 15:21
  • 7
    I'm not saying it's mad to forbid operator overloading. I'm saying it's mad to forbid it for that reason, which is both easily overcome, and far less significant than other plausible reasons. It's an exaggeration to say that in C++ you cannot trust anything - you do have to be cautious in what you expect, though. Most of the things which in C++ are "unpredictable" by Java programmers wouldn't even compile in Java or C in the first place. Assignment/conversion are the truly awkward ones. I've never shared the panic on seeing two objects added together, but I appreciate that it's not mad. – Steve Jessop Jan 21 '10 at 15:43
10

Yes. && and || short circuit in C and C++; it is guaranteed by the standard.

See also: Is short-circuiting logical operators mandated? And evaluation order?

Community
  • 1
  • 1
Crashworks
  • 40,496
  • 12
  • 101
  • 170