It's mostly fashion, but also a combination of consistency and misunderstanding.
When iterators came along, and more and more people started realising that ++it
could be cheaper than it++
, for some more-complex-than-a-pointer iterator it
, they started getting into the habit of pre-incrementing everywhere, to save what they perceived to be unnecessary copies in the semantics of a post-increment (the original value would have to be stored temporarily for returning after the increment occurs). They'd then do that for integers too, because why not?
In reality, there's nothing to save. Your compiler is perfectly capable of spotting that the evaluated result is never used here, so ++it
and it++
in this context have exactly identical semantics (unless there is some non-idiomatic side effect in the definition of the operator). This is particularly the case for a simple integer i
.
It doesn't hurt, of course, and it does eliminate the risk that you've missed some weird angle that does result in an unnecessary copy. It's a good practice. It's just that it hasn't come about out of necessity.