Why is x=x++ undefined?
- by ugoren
It's undefined because the it modifies x twice between sequence points. The standard says it's undefined, therefore it's undefined.
That much I know.
But why?
My understanding is that forbidding this allows compilers to optimize better. This could have made sense when C was invented, but now seems like a weak argument.
If we were to reinvent C today, would we do it this way, or can it be done better?
Or maybe there's a deeper problem, that makes it hard to define consistent rules for such expressions, so it's best to forbid them?
So suppose we were to reinvent C today. I'd like to suggest simple rules for expressions such as x=x++, which seem to me to work better than the existing rules.
I'd like to get your opinion on the suggested rules compared to the existing ones, or other suggestions.
Suggested Rules:
Between sequence points, order of evaluation is unspecified.
Side effects take place immediately.
There's no undefined behavior involved. Expressions evaluate to this value or that, but surely won't format your hard disk (strangely, I've never seen an implementation where x=x++ formats the hard disk).
Example Expressions
x=x++ - Well defined, doesn't change x.
First, x is incremented (immediately when x++ is evaluated), then it's old value is stored in x.
x++ + ++x - Increments x twice, evaluates to 2*x+2.
Though either side may be evaluated first, the result is either x + (x+2) (left side first) or (x+1) + (x+1) (right side first).
x = x + (x=3) - Unspecified, x set to either x+3 or 6.
If the right side is evaluated first, it's x+3. It's also possible that x=3 is evaluated first, so it's 3+3. In either case, the x=3 assignment happens immediately when x=3 is evaluated, so the value stored is overwritten by the other assignment.
x+=(x=3) - Well defined, sets x to 6.
You could argue that this is just shorthand for the expression above.
But I'd say that += must be executed after x=3, and not in two parts (read x, evaluate x=3, add and store new value).
What's the Advantage?
Some comments raised this good point.
It's not that I'm after the pleasure of using x=x++ in my code. It's a strange and misleading expression. What I want is to be able to understand complicated expressions.
Normally, a complicated expression is no more than the sum of its parts. If you understand the parts and the operators combining them, you can understand the whole.
C's current behavior seems to deviate from this principle. One assignment plus another assignment suddenly doesn't make two assignments.
Today, when I look at x=x++, I can't say what it does. With my suggested rules, I can, by simply examining its components and their relations.