Increment/decrement operators

Increment/decrement operators increment or decrement the value of the object.

# Declarations

A& operator++(A&)
bool& operator++(bool&)

(deprecated)(until C++17)

P& operator++(P&)
A& operator--(A&)
P& operator--(P&)

# Notes

Because of the side-effects involved, built-in increment and decrement operators must be used with care to avoid undefined behavior due to violations of sequencing rules.

Because a temporary copy of the object is constructed during post-increment and post-decrement, pre-increment or pre-decrement operators are usually more efficient in contexts where the returned value is not used.

# Example

#include <iostream>
 
int main()
{
    int n1 = 1;
    int n2 = ++n1;
    int n3 = ++ ++n1;
    int n4 = n1++;
//  int n5 = n1++ ++;   // error
//  int n6 = n1 + ++n1; // undefined behavior
    std::cout << "n1 = " << n1 << '\n'
              << "n2 = " << n2 << '\n'
              << "n3 = " << n3 << '\n'
              << "n4 = " << n4 << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 2855C++98usual arithmetic conversions are applied for built-in pre-increment andpre-decrement, but were not applied for their postfix counterparts[1]also applied
CWG 2901C++98lvalue-to-rvalue conversions were not appliedfor built-in post-increment and post-decrementapplied

# See also