Difference between i++ ++i

closed account (EwCjE3v7)
Is there a difference between putting the '++' before and after, or is it the same/

1
2
  ++i;
  i++;
Yes - there is a huge difference, but in most cases the compiler optimizes it out.

i++ is called post-increment - it is the result of making a copy of i, incrementing i, and then returning the copy as the result of the expression. If the result of the expression is not used though then the compiler almost always optimizes the copy out. The copy is an rvalue and cannot be bound to a non-const reference.

++i is post increment, and should be what you write 99% of the time. It increments i and returns a reference to i, no copy involved ever. The returned value is an lvalue and can be bound to a non-const reference.

The same rules apply for --i and i--, as well as with non-integer types (e.g. standard library iterators), though it is harder for the compiler to optimize post-increment and post-decrement with non-primitive types.
Last edited on
closed account (EwCjE3v7)
Thank you for your quick response and the answer.
Topic archived. No new replies allowed.