If I want to assign a new value to a variable and check if the new value is the same as the old value, I would tend to avoid using temporary variables. Could I do something like this:
if (oldval == (oldval = new_value()))
{
... do something
}
?
Would this behaviour be well-defined or is the evaluation priority language or compiler dependant? I could try this and see the result, but it would not guarantee that it will work on other systems as well. I am doing it in C++, so this is the language which interests me the most, but if the behaviour is consistent (or inconsistent) amongst other languages, I would like to know.
Thanks!
The order of evaluation of the operand of ==
is not defined (I am assuming that ==
is not overloaded). It is not guaranteed whether oldval
will evaluate first or oldval = new_value()
will. Behavior is undefined in this case.
Avoid writing such expressions that access the value of a variable and also modify that variable elsewhere in the expression.
NOTE: Only the operators, ,
, &&
, ||
and ?:
guarantee that the operand evaluation takes place from left to right. So, there exist sequence point:
- Between evaluation of the left and right operands of the
&&
(logical AND),||
(logical OR) (as part of short-circuit evaluation), and comma operators.- Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand.
This would be undefined behaviour because the write to oldval
that occurs in oldval = new_value()
is unsequenced with respect to a value computation that uses the value of oldval
(i.e. the evaluation of the left-hand side of ==
).
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