Why does the expression 'a++ += b' give an error?

This is an error1:

a++ += b

because a++ returns a temporary (a pr-value) the language forbids you to modify since it is discarded as soon as the full expression has been evaluated. This is a kind of fail safe.


My understanding of this had been that this expression would first evaluate a+b, store that value in a and then increment it.

No it doesn’t. According to operator precedences, ++ evaluates before +=.


1) This answer assumes a and b are builtin types or well-behaved user-defined class types.

Leave a Comment