Re: expressions and order of evaluation
Taras_96 wrote:
[..]
i = i++;
The value of (i++) is the original value of i. As above, the LHS
evaluates to an l-value (which doesn't change regardless of when the
side effect is applied). So why wouldn't the value on the RHS (which
AFAIK is the original value of i regardless of when the side effect is
applied) get assigned to the l-value regardless of when the side
effect is applied?
What do you mean "regardless"? Assigning the value to 'i' (whichever
value it is) and incrementing it are both *side effects*. The order in
which they take place is unspecified. Since they both change the value
of the same object, the behaviour is undefined.
> As you mentioned, the value of i++ (which is the
original value of i) must be calculated before it is 'used' (ie:
assigned), which has been done.
The undefinedness of the expression in not in the inability to determine
what value is on the RHS of the assignment subexpression, but what will
the side effects of the whole expression be.
The expression
a = b++
if we involve sequence points (SP) would look like this:
SP1 (before the expression)
\
+---------------------------+--------------------------+
| old value of 'b' is taken | |
+---------------------------+ ov_b is assigned to 'a' |
| lvalue of 'a' is figured | |
+---------------------------+--------------------------+
| | lvalue b is incremented |
+---------------------------+--------------------------+
/
(after the expression) SP2
At the second sequence point both side effects have taken place.
Figuring out the lvalue of 'a' can be a NOP. The main point, however,
is that the increment of 'b' and assignment to 'a' are concurrent, from
the sequencing point of view (the new Standard takes more care about
those definitions, and I may not be using the right terms here). IOW
between SP1 and SP2 *two* objects change value. When exactly is unknown
(unspecified) and if those two objects are actually the same object,
then evaluating such an expression has UB.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask