1

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

Google's C++ Style Guide[1] states that using preincrement(++i) is often more efficient than using postincrement(i++) so it's preferable as the meaning is the same if the return value is ignored.

If you're talking about a native type the difference is probably negligible in almost every case, but if you have an overloaded class I understand it can make a difference. Anyway, considering the case that the return value doesn't matter, isn't this the perfect target for a compiler optimization? Does it happen in modern C++ compilers? If not, why?

[1] http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Preincrement_and_Predecrement#Preincrement_and_Predecrement

Community
  • 1
  • 1
Thiago Moraes
  • 617
  • 1
  • 10
  • 22
  • 3
    no in 99% of all cases, google style guides for C++ are very bad in several categories. – Stephan Dollberg Dec 01 '12 at 20:47
  • 1
    I like the preincrement because is easier to read: "++" is read as "increment", so "++i" means "increment i". – rodrigo Dec 01 '12 at 20:55
  • 1
    I think who voted teh closing had been to fast: the question clearly says "THIS DAYS", and cannot be considered a dup. of a question asked TWO YEARS AGO, especially after a new C++ standard had been developed and certain compiler optimization had been standardized themselves. After the introduction of C++11 this question make perfectly sense, and the answers (especially if based on tests) can be surprising. – Emilio Garavaglia Dec 01 '12 at 21:02
  • The compiler can't replace `object++` with `++object` for user-defined classes because it cannot know if they're equivalent. They could have different side-effects, for example. – Daniel Fischer Dec 01 '12 at 21:03
  • 1
    @Emilio - Nothing has changed, and it still doesn't matter much which version you use. Google's style guide is used by Google because they *have to* when supporting an ancient code base, not because they want to or beause it contains lots of good rules. It doesn't. – Bo Persson Dec 01 '12 at 21:17
  • @BoPersson: I don't know if it was for you intentional, but to me, you are enforcing my opinion. "This days" are different. But Google does not care (for reasons good to them, not necessarily always) – Emilio Garavaglia Dec 01 '12 at 21:28

0 Answers0