Click here to Skip to main content
12,826,676 members (28,347 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.2K views
1 bookmarked
Posted 2 Mar 2010

Increment/Decrement operators

, 2 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Stefan_Lang
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

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
Yoldas Askan15-Sep-14 8:00
memberYoldas Askan15-Sep-14 8:00 
QuestionIs there any standard that requires creation of a temp? Pin
supercat92-Mar-10 7:59
membersupercat92-Mar-10 7:59 
AnswerRe: Is there any standard that requires creation of a temp? Pin
Tim Craig2-Mar-10 9:49
memberTim Craig2-Mar-10 9:49 
AnswerRe: Is there any standard that requires creation of a temp? Pin
dwilliss2-Mar-10 16:44
memberdwilliss2-Mar-10 16:44 
GeneralRe: Is there any standard that requires creation of a temp? Pin
Tim Craig2-Mar-10 22:38
memberTim Craig2-Mar-10 22:38 
GeneralRe: Is there any standard that requires creation of a temp? Pin
Stefan632-Mar-10 23:25
memberStefan632-Mar-10 23:25 
GeneralRe: Is there any standard that requires creation of a temp? Pin
supercat93-Mar-10 8:35
membersupercat93-Mar-10 8:35 
GeneralRe: Is there any standard that requires creation of a temp? [modified] Pin
Stefan634-Mar-10 0:22
memberStefan634-Mar-10 0:22 
Interesting. I haven't thought of that. This goes along the same lines as STL algorithms that require only a 'less' function or operator<() to work on ordered sets instead of the whole set of comparison operators.

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.

Consider the example I mentioned above - iterators for the purpose of navigating table records - a copy might imply a lot more than copying an index, a pointer, or some data structure that works in that way - in case of databases you might need to a acquire a read lock and be granted the permission to hold onto multiple iterators. I am no database specialist, but I have seen so many weird and unusual constructs for all kinds of reasons I am no longer willing to make *any* assumptions about the inner workings of just about any database function. Nor should a compiler!

P.S.:
Nobody stops you from implementing quite different semantics into your prefix and postfix operators. For instance I once considered creating an iterator for a two-dimensional list of structures and wondered whether I should be using postfix and prefix operators to navigate horizontally and vertically, respectively. In the end I decided against it, but other people might have less scruples Wink | ;)
modified on Thursday, March 4, 2010 5:29 AM

GeneralRe: Is there any standard that requires creation of a temp? Pin
supercat94-Mar-10 6:00
membersupercat94-Mar-10 6:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 2 Mar 2010
Article Copyright 2010 by Stefan_Lang
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid