Post increment and decrement

weaselcat via Digitalmars-d digitalmars-d at puremagic.com
Sat Mar 14 03:30:55 PDT 2015


On Saturday, 14 March 2015 at 10:11:27 UTC, Casper Færgemand 
wrote:
> What exactly is the downside of i++? Suppose the compiler 
> doesn't lower it and I use the expression alone, what is the 
> potential damage?

AFAIK, there's no guarantee in C/C++ that i++ will be optimized 
to ++i at all.

i++ requires a temporary, this can be trivially optimized away in 
most cases but an overloaded post-increment can make this much 
more difficult

an example is iterators in C++. Post increment requires you to 
make a copy of the iterator, increment the current one, and 
evaluate the copy. Pre-increment just requires you to increment 
the iterator then evaluate it.(This might not even be true 
anymore, compilers are pretty smart nowadays.)

In D, I don't think it matters that much as already discussed. 
It's just good form, I believe.


More information about the Digitalmars-d mailing list