quote:Original post by DrPizza
With primitives, the compiler can safely transform postincrement to preincrement when it knows the previous value is unused.
That's not the whole reason, because the post-incremented variable can be within an expression too. What postincrement does is to use the current value of the variable inside the expression, and increment the variable after the expression.
Example:
5*(x++)+2;
<=>
5*x+2;
++x;
The temporary problem comes only when user defined types try to mimic this behaviour, which is impossible without a temp.
quote:Original post by petewood
baseline, there will be a temporary of come kind.
I don't see any in the above simplification I made, even though it post-increments correctly. It simply delays the incrementation until the expression has been evaluated.
Of course there's a slight possibility that a compiler exists which uses a temp for this. But I doubt it, since it's so trivial to move the increment operation a bit lower.
It works with branching code too, since comparing is separate from the branch operation.
if (x++) {}
becomes
cmp ax, 0
inc ax, 1
je after_if
(or something, ages since I've written x86 asm)
[edited by - civguy on November 21, 2002 2:48:06 PM]