Increment/decrement operators increment or decrement the value of the object.
Operator name Syntax Overloadable Prototype examples (for class T) Inside class definition Outside class definition pre-increment++a
Yes T& T::operator++(); T& operator++(T& a); pre-decrement --a
Yes T& T::operator--(); T& operator--(T& a); post-increment a++
Yes T T::operator++(int); T operator++(T& a, int); post-decrement a--
Yes T T::operator--(int); T operator--(T& a, int);
The prefix increment and decrement expressions have the form
++
expression --
expression
1) prefix increment (pre-increment)
2) prefix decrement (pre-decrement)
[edit] Built-in prefix operators 1)The expression
++xis equivalent to
x += 1, with the following exceptions:
The expression
--xis equivalent to
x -= 1, with the following exceptions:
In overload resolution against user-defined operators, for every optionally volatile-qualified arithmetic type A
other than bool, and for every optionally volatile-qualified pointer P
to optionally cv-qualified object type, the following function signatures participate in overload resolution:
A& operator++(A&)
bool& operator++(bool&)
(deprecated)(until C++17)P& operator++(P&)
A& operator--(A&)
P& operator--(P&)
[edit] Postfix operatorsThe postfix increment and decrement expressions have the form
expression++
expression --
1) postfix increment (post-increment)
2) postfix decrement (post-decrement)
[edit] Built-in postfix operatorsThe result of postfix increment or decrement is the value obtained by applying the lvalue-to-rvalue conversion to expression (before modification). The type of the result is the cv-unqualified version of the type of expression.
If expression is not a modifiable lvalue of an arithmetic type other than (possibly cv-qualified) bool(since C++17), or a pointer to a complete object type, the program is ill-formed.
If the type of expression is volatile-qualified, the increment or decrement is deprecated.
(since C++20)1) The value of expression is modified as if it were the operand of the prefix ++
operator.
2) The value of expression is modified as if it were the operand of the prefix --
operator.
The value computation of a postfix increment or decrement is sequenced before the modification of expression. With respect to an indeterminately-sequenced function call, the operation of a postfix increment or decrement is a single evaluation.
[edit] OverloadsIn overload resolution against user-defined operators, for every optionally volatile-qualified arithmetic type A
other than bool, and for every optionally volatile-qualified pointer P
to optionally cv-qualified object type, the following function signatures participate in overload resolution:
A operator++(A&, int)
bool operator++(bool&, int)
(deprecated)(until C++17)P operator++(P&, int)
A operator--(A&, int)
P operator--(P&, int)
[edit] 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'; }
Output:
n1 = 5 n2 = 2 n3 = 4 n4 = 4[edit] 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.
[edit] Standard libraryIncrement and decrement operators are overloaded for many standard library types. In particular, every LegacyIterator overloads operator++ and every LegacyBidirectionalIterator overloads operator--, even if those operators are no-ops for the particular iterator.
overloads for arithmetic types increments or decrements the atomic value by onestd::atomic<T>
) [edit] increments or decrements the tick count
std::chrono::duration<Rep,Period>
) [edit] overloads for iterator types advances the iterator
std::raw_storage_iterator<OutputIt,T>
) [edit] advances or decrements the reverse_iterator
std::reverse_iterator<Iter>
) [edit] advances or decrements the move_iterator
std::move_iterator<Iter>
) [edit] no-op
std::front_insert_iterator<Container>
) [edit] no-op
std::back_insert_iterator<Container>
) [edit] no-op
std::insert_iterator<Container>
) [edit] advances the iterator
std::istream_iterator<T,CharT,Traits,Distance>
) [edit] no-op
std::ostream_iterator<T,CharT,Traits>
) [edit] advances the iterator
std::istreambuf_iterator<CharT,Traits>
) [edit] no-op
std::ostreambuf_iterator<CharT,Traits>
) [edit] advances the iterator to the next match
std::regex_iterator<BidirIt,CharT,Traits>
) [edit] advances the iterator to the next submatch
std::regex_token_iterator<BidirIt,CharT,Traits>
) [edit] [edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior CWG 2855 C++98 usual arithmetic conversions are applied for built-in pre-increment anda = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b
++a
--a
a++
a--
+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b
!a
a && b
a || b
a == b
a != b
a < b
a > b
a <= b
a >= b
a <=> b
a[...]
*a
&a
a->b
a.b
a->*b
a.*b
a(...)
commaa, b
conditionala ? b : c
Special operatorsstatic_cast converts one type to another related type
dynamic_cast converts within inheritance hierarchies
const_cast adds or removes cv-qualifiers
reinterpret_cast converts type to unrelated type
C-style cast converts one type to another by a mix of static_cast, const_cast, and reinterpret_cast
new creates objects with dynamic storage duration
delete destructs objects previously created by the new expression and releases obtained memory area
sizeof queries the size of a type
sizeof... queries the size of a pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4