Why is ++i considered an l-value, but i++ is not?

yesraaj picture yesraaj · Dec 16, 2008 · Viewed 10.6k times · Source

Why is ++i is l-value and i++ not?

Answer

mackenir picture mackenir · Dec 16, 2008

Other people have tackled the functional difference between post and pre increment.

As far as being an lvalue is concerned, i++ can't be assigned to because it doesn't refer to a variable. It refers to a calculated value.

In terms of assignment, both of the following make no sense in the same sort of way:

i++   = 5;
i + 0 = 5;

Because pre-increment returns a reference to the incremented variable rather than a temporary copy, ++i is an lvalue.

Preferring pre-increment for performance reasons becomes an especially good idea when you are incrementing something like an iterator object (eg in the STL) that may well be a good bit more heavyweight than an int.