C++ Reference ยท Operators
C++ increment and decrement operators
Reference for the C++ increment and decrement operators ++ and --: prefix versus postfix, return values, iterators, overloading and unsequenced pitfalls.
++ adds one to its operand and -- subtracts one. Each comes in two forms. The prefix form (++i) changes the object and yields the new value as an lvalue. The postfix form (i++) changes the object but yields a copy of the old value.
They work on integers, floating-point numbers, pointers and iterators, and you can overload them for your own types. Prefer the prefix form when you do not need the old value.
int i = 5; int a = i++; // a == 5, i == 6 int b = ++i; // b == 7, i == 7
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
++x --x // prefix x++ x-- // postfix T& T::operator++(); // prefix, member T T::operator++(int); // postfix, member (unused int distinguishes it) T& operator++(T&); // prefix, free function T operator++(T&, int); // postfix, free function
| Name | Description |
|---|---|
++x, --x |
Modify x and yield x itself (an lvalue). |
x++, x-- |
Modify x and yield a prvalue copy of the previous value. |
int parameter |
Present only in postfix overloads. It is never used and callers pass 0. |
Errors / undefined behavior: the operand must be a modifiable lvalue (5++ and (a + b)++ do not compile). Incrementing a signed integer past its maximum is undefined behavior. Incrementing a pointer beyond one past the end of its array is undefined behavior. Modifying a variable twice, or modifying and reading it, in one expression without sequencing (i++ + ++i, f(i, i++)) is undefined behavior or at best unspecified.
Usage notes
- Prefer prefix: for built-in types the compiler generates identical code, but for iterators and other class types the postfix form must make a copy.
for (auto it = c.begin(); it != c.end(); ++it)is the habit to build. - Return types: prefix should return
T&; postfix returnsTby value. Returning a reference from postfix would dangle. - Postfix in terms of prefix: implement postfix as "copy, call prefix, return copy", as in the example, so the two never disagree.
bool:++on aboolwas removed in C++17.--on aboolhas never been allowed.- Enums: scoped enums have no
++; write a small overload if you want to step through them. - Unsequenced modifications: do not modify the same variable twice in one expression (
i++ + ++i): the operands of+are unsequenced, so this is undefined behavior. - Atomics:
std::atomic<int>supports++and--as atomic read-modify-write operations. A plainintshared between threads does not. - Iterators: only forward iterators and stronger must support
++; only bidirectional ones support--.