So I had a discussion with a colleague at work regarding the order of evaluation execution. Consider this conditional:
if (a > b && ref++) { do something }
We both agree that as per C standard the order is from left to right. However, the disagreement is that despite the order of evaluation, some instructions whose data will eventually be evaluated may be speculatively executed and then remain in this state despite the lazy evaluation.
For instance, ref
is incremented before evaluating a > b
, and the evaluation still happens left to right, that is the actual jump greater than
instruction for both a > b
and rev > 0
are executed in-order left to right, and abandoned at the first instance when !true. Now this goes against the whole concept of lazy evaluation, but what if ref
is a stream of instructions, an inlined function maybe. It would make more sense to start executing some instructions speculatively. The concern is: are those instructions committed, even half way through.
Thoughts?
Operator &&
(as well as operator ||
) are special in a sense as they guarantee "short-cut-evaluation" from left to right. This means that for a condition like expr1 && expr2
, expr1
will always be evaluated first, and - even more - that expr2
will not get evaluated at all if expr1
already evaluated to false. This is not an optimization, this is guaranteed by the language.
Hence, in if (a > b && ref++)
, expression ref++
will be evaluated (including it's side effect of incrementing ref
) if and only if a > b
evaluates to true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With