[Solved] Why does ++x++ give compile error?


The result of the post-increment expression, a++ is an rvalue; a temporary with the value that a had before incrementing. As an rvalue, you can use its value, but you can’t modify it. Specifically, you can’t apply pre-increment it, as the compiler says.

If you were to change the precedence to do the pre-increment first:

(++a)++

then this would compile: the result of pre-increment is an lvalue denoting the object that’s been modified. However, this might have undefined behaviour; I’m not sure whether the two modifications and the various uses of the value are sequenced.

Summary: don’t try to write tricky expressions with multiple side-effects.

2

solved Why does ++x++ give compile error?