Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++
Tip/Trick

Increment/Decrement operators

Rate me:
Please Sign up or sign in to vote.
4.40/5 (3 votes)
2 Mar 2010CPOL 19.2K   1   9
When incrementing or decrementing a variable, favor prefix operators over postfix operators if you do not need the value of the expression.Example:void foo(std::vector intvec){ for (std::vector::iterator it = intvec.begin(); it != intvec.end(); ++it) { // do something ...
When incrementing or decrementing a variable, favor prefix operators over postfix operators if you do not need the value of the expression.

Example:
void foo(std::vector<int> intvec)
{
   for (std::vector<int>::iterator it = intvec.begin(); it != intvec.end(); ++it)
   {
      // do something
   }
}


The reason for this is that postfix operators (such as it++) need to create a copy of the original variable. This copy is being used as a placeholder that later can be used as the value of the postfix expression. A prefix operator does not need to do that, as the value of a prefix increment or decrement expression is the value of the variable after the operation.

The cost of creating an additional temp might be trivial for integral types, but often ++/-- operators are defined for non-trivial types, and by their very nature they are being used in loop constructs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
Graduated at TU Darmstadt in Math & CS, with a heavy focus on CAD/CAM

Programming and designing applications in C++ in the areas AI, real-time programming, client-server applications and CAD/CAM since 1985.

Personal interests: AI, computer graphics, games, reading

Comments and Discussions

 
GeneralMy vote of 1 Pin
Yoldas Askan15-Sep-14 7:00
Yoldas Askan15-Sep-14 7:00 
QuestionIs there any standard that requires creation of a temp? Pin
supercat92-Mar-10 6:59
supercat92-Mar-10 6:59 
AnswerRe: Is there any standard that requires creation of a temp? Pin
Tim Craig2-Mar-10 8:49
Tim Craig2-Mar-10 8:49 
AnswerRe: Is there any standard that requires creation of a temp? Pin
dwilliss2-Mar-10 15:44
dwilliss2-Mar-10 15:44 
GeneralRe: Is there any standard that requires creation of a temp? Pin
Tim Craig2-Mar-10 21:38
Tim Craig2-Mar-10 21:38 
GeneralRe: Is there any standard that requires creation of a temp? Pin
Stefan_Lang2-Mar-10 22:25
Stefan_Lang2-Mar-10 22:25 
GeneralRe: Is there any standard that requires creation of a temp? Pin
supercat93-Mar-10 7:35
supercat93-Mar-10 7:35 
GeneralRe: Is there any standard that requires creation of a temp? [modified] Pin
Stefan_Lang3-Mar-10 23:22
Stefan_Lang3-Mar-10 23:22 
GeneralRe: Is there any standard that requires creation of a temp? Pin
supercat94-Mar-10 5:00
supercat94-Mar-10 5:00 
Yet, current compilers will not automagically create a postfix operator out of a prefix operator if none is defined. In part because a postfix operator might have been omitted by design, and in part because it might require more than a simple copy operation - not to mention that some types might explicitely disallow copying by making copy constructors and copy-assignment operators private.


I guess if a language specification provides that the prefix and postfix operators run separate methods, and doesn't allow the language to write one in terms of the other, there really isn't any way the compiler could write one in terms of the other. Too bad the C++ standard requires a coder to manually implement a postincrement operator in order to use it; otherwise if the semantics of pre-inc versus post-inc were the same for objects as for simple types it would have been sufficient for a compiler to keep a temp reference and use it to perform the increment immediately before the next sequence point (when parsing the expression, replace every post-increment operation with an operation to grab a reference to the thing that should be incremented; when the next sequence point is reached, use those references to perform the increments). I guess if postincrement is done with a method, the increment must actually be performed before the result of the expression is used, but that behavior isn't mandated with scalar and pointer types.

Thinking about it, perhaps the most useful thing would have been to allow different methods to be called based upon what was being done with the return value. With databases, for example, there are three distinct ways one might want to go to the next record:

  1. Advance to the next record, without fetching anything.
  2. Fetch the present record, and advance to the next.
  3. Advance to the next record and fetch it.

Doing an advance and fetch together, in either order, may be faster than doing one operation, waiting for it to complete, and then doing the other, but if the fetch isn't needed, omitting it may be better yet. I wonder if there's any sensible way to allow for that?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.