Possible Duplicates:
Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?
Incrementing in C++ – When to use x++ or ++x?
i++ vs. ++i
When is this used in real scenarios?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The obvious is when you want the old value returned, you use post-increment.
The more subtle things are that pre-increment should really never be slower and could be faster due to the lack of creating a temporary and returning the old value when using post-increment.
A real scenario for using post-increment in C++ is when erasing from standard containers. For example:
In response to the compiler optimization, it is true yet I think it’s always important to convey as precisely as possible what you’re trying to accomplish. If you don’t need the returned value from x++, then don’t ask for it. Also, I’m not sure you would always get the same optimization if the type your incrementing is not a simple type. Think iterators that are not just plain pointers. In cases such as this, your mileage may vary with regard to optimization. In short, always pre-increment unless you need the returned (i.e. old value) of the post-increment operator.