Wrong answer:
Because value++
is a temporary variable that holds the old value of value
. You can't ++
it.
You also can't write 15++
! It's similar. The former is a temporary variable, the later is a constant, none of which you can increment.
Correction: Since this answer got accepted, I am not going to change the original post, but since people will read it, I will post the correction here.
First off, I am not saying constants and temporary objects are the same concept. I was just trying to say temporary objects are not l-values, like constants are not, without actually using the word l-value.
About value++++
, it is not an error. I just tested it with my g++ 4.5 and it works fine. In other answers you can read:
From section 3.10.10 of the 2003 standard:
An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [Example: a member function called for an object (9.3) can modify the object. ]
What is inherently wrong about what you are doing is this:
Let's say value
holds 10
. The first value++
changes value
to 11
but returns a temporary object containing 10
. Then you ++
it which changes the temporary value (that you never access) to 11
, but again returns a temporary object that contains 10
. So value++++
behaves exactly like value++
except it does some unnecessary work.