Tim Johnson wrote:
I'm having trouble understanding this part of the expression
evaluation rules ('6.5 Expressions', second item, C99 spec; the C++
wording is presumably identical):
>>Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.
What precisely does the second part of this statement mean?
If you write to some X during evaluation, that must be to work out
what value to write, not for some other reason. So the auto-increment
operations are legal (which is good, otherwise they'd be useless)
but things like:
The example states that "a[i++] = i" is invalid.
are not.
You can read the value as many times as you like while doing this,
so `i = i + i` is legal. (I infer this from the fact that there's
no language that forbids it here and it would otherwise be allowed.)
The phrase "the prior value shall be read only to determine the value
to be stored" means that the write can't happen [1] until all the reads
have happened -- otherwise the value to store couldn't be determined --
and hence there's no undefinedness loopholes from timing problems. In
`a[i++] = i`, you don't know whether the value assigned could be taken
from `i` before or after (or during ...) the increment.
But what about, for
example:
a = (b = c) || (c = d);
a = (c = d) || (b = c);
Are these valid or not?
These are OK because || has a specific evaluation rule; the left
operand is evaluated and then there's a sequence point.
Could someone give me a more verbose and
precise interpretation of the second part of the statement?
Which bit do you need precision engineering on?
[1] Or at least the implementation is obliged to make it /look/
like it can't/didn't happen.
--
Chris "Perikles triumphant" Dollin
Nit-picking is best done among friends.