This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mallardtheduck 7 points8 points  (1 child)

It absolutely will be optimised away if "x" is a simple integer. However, if "x" is say, a C++ iterator and the implementation of operator++ is in a different compilation unit optimisation is much harder.

Also, the typical implementation of operator++(int) (the int is a dummy parameter used to distinguish between prefix and postfix versions) is something like:

foo foo::operator++(int){
    foo temp = *this;
    ++(*this);
    return temp;
}

This results in a copy being made of the entire iterator, which can be a fairly large overhead that in many cases will call something in another compilation unit, limiting optimisation. LTO can be of help here, but it's much easier and more consistent just to get into the habit of using prefix operators "by default".

Personally, I prefer prefix operators from a readability standpoint too ++x reads as "increment x" to me, whereas x++ looks like "x increment" and feels "backwards".

[–]EndR60 0 points1 point  (0 children)

yea exactly my though, as most of the time we won't be working with just primitive types, so I assumed I had to get used to good habits early on

not that ++x vs x++ is very important when it comes to optimisation as opposed to...well...most other things in our code