Is Short Circuit Evaluation guaranteed In C++ as it is in Java?
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());
Solution 1:
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.
Solution 2:
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?