First of all, I believe that && means an embedded version of the logical AND operator.
I think that the compiler can legitimately perform evaluations from a sub-expression of the right side of the && operator before it completes the evaluation of the left side, but in a way that does not alter the semantics of the full expression.
For your example, the C ++ compiler is allowed to introduce reordering under the following conditions:
a is a primitive pointer (i.e. its type is not a class that overloads operator* ).b is a primitive pointer (i.e. its type is not a class that overloads operator-> )b , as you know, is sought regardless of the value of *a
If 1. fails, then the user operator* may have the side effect of changing the value of b->foo .
If 2. is not executed, then the user-defined operator-> can change the value of *a or throw away or produce another observable side effect (for example, print something) that should not be displayed had *a evaluated as false .
If 3. cannot be proved using static analysis, then reordering will result in undefined behavior that is not in the original program.
Compiler
C, naturally, has to execute only the 3rd check.
In fact, even if *a and b->foo are associated with operator overloading, the C ++ compiler can still reorder the instructions when these statements can be embedded and the compiler does not detect anything dangerous.
source share