Difference between revisions of "cpp/language/operator incdec"
m (navbar) |
m (link lvalue and object type) |
||
Line 60: | Line 60: | ||
{{dcl end}} | {{dcl end}} | ||
− | The operand of a built-in prefix increment or decrement operator must be a modifiable | + | The operand of a built-in prefix increment or decrement operator must be a modifiable (non-const) {{rlp|value category|lvalue}} of non-boolean arithmetic type or pointer to complete {{rlp|type|object type}}. For these operands, the expression {{c|++x}} is exactly equivalent to {{c|1=x+=1}}, and the expression {{c|--x}} is exactly equivalent to {{c|1=x-=1}}, that is, the result is the updated operand, returned as lvalue, and all arithmetic conversion rules and pointer arithmetic rules defined for [[cpp/language/operator_arithmetic|arithmetic operators]] apply. |
If the operand of the preincrement operator is of type {{c|bool}}, it is set to {{c|true}} {{mark deprecated}}. | If the operand of the preincrement operator is of type {{c|bool}}, it is set to {{c|true}} {{mark deprecated}}. | ||
Line 75: | Line 75: | ||
{{dcl end}} | {{dcl end}} | ||
− | The operand of a built-in postfix increment or decrement operator must be a modifiable | + | The operand of a built-in postfix increment or decrement operator must be a modifiable (non-const) {{rlp|value category|lvalue}} of non-boolean arithmetic type or pointer to complete {{rlp|type|object type}}. The result is a prvalue, which is a copy the original value of the operand. As a side-effect, this operator modifies the value of its argument {{tt|arg}} as if by evaluating {{c|1=arg += 1}} or {{c|1=arg -= 1}}, for increment and decrement respectively. All arithmetic conversion rules and pointer arithmetic rules defined for [[cpp/language/operator_arithmetic|arithmetic operators]] apply. |
If the operand of the postincrement operator is of type {{c|bool}}, it is set to {{c|true}} {{mark deprecated}}. | If the operand of the postincrement operator is of type {{c|bool}}, it is set to {{c|true}} {{mark deprecated}}. |
Revision as of 09:32, 27 January 2014
Increment/decrement operators increments or decrements 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); |
|
Contents |
Explanation
pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result.
post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.
Built-in prefix 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) | |
P& operator++(P&) |
||
A& operator--(A&) |
||
P& operator--(P&) |
||
The operand of a built-in prefix increment or decrement operator must be a modifiable (non-const) lvalue of non-boolean arithmetic type or pointer to complete object type. For these operands, the expression ++x is exactly equivalent to x+=1, and the expression --x is exactly equivalent to x-=1, that is, the result is the updated operand, returned as lvalue, and all arithmetic conversion rules and pointer arithmetic rules defined for arithmetic operators apply.
If the operand of the preincrement operator is of type bool, it is set to true (deprecated).
Built-in postfix 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) | |
P operator++(P&, int) |
||
A operator--(A&, int) |
||
P operator--(P&, int) |
||
The operand of a built-in postfix increment or decrement operator must be a modifiable (non-const) lvalue of non-boolean arithmetic type or pointer to complete object type. The result is a prvalue, which is a copy the original value of the operand. As a side-effect, this operator modifies the value of its argument arg
as if by evaluating arg += 1 or arg -= 1, for increment and decrement respectively. All arithmetic conversion rules and pointer arithmetic rules defined for arithmetic operators apply.
If the operand of the postincrement operator is of type bool, it is set to true (deprecated).
Example
#include <iostream> int main() { int n = 1; int n2 = ++n; int n3 = ++ ++n; int n4 = n++; // int n5 = n++ ++; // compile error // int n5 = n + ++n; // undefined behavior std::cout << "n = " << n << '\n' << "n2 = " << n2 << '\n' << "n3 = " << n3 << '\n' << "n4 = " << n4 << '\n'; }
Output:
n = 5 n2 = 2 n3 = 4 n4 = 4
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 the operation, pre-increment or pre-decrement operators are usually more efficient in contexts where the returned value is not used.
Standard library
Increment and decrement operators are overloaded for many standard library types. In particular, every Template:concept overloads operator++ and every Template:concept overloads operator--, even if those operators are no-ops for the particular iterator.
overloads for arithmetic types | |
increments or decrements the atomic value by one (public member function of std::atomic<T> )
| |
increments or decrements the tick count (public member function of std::chrono::duration<Rep,Period> )
| |
overloads for iterator types | |
advances the iterator (public member function of std::raw_storage_iterator<OutputIt,T> )
| |
advances or decrements the iterator (public member function of std::reverse_iterator<Iter> )
| |
advances or decrements the iterator (public member function of std::move_iterator<Iter> )
| |
no-op (public member function of std::front_insert_iterator<Container> )
| |
no-op (public member function of std::back_insert_iterator<Container> )
| |
no-op (public member function of std::insert_iterator<Container> )
| |
advances the iterator (public member function of std::istream_iterator<T,CharT,Traits,Distance> )
| |
no-op (public member function of std::ostream_iterator<T,CharT,Traits> )
| |
advances the iterator (public member function of std::istreambuf_iterator<CharT,Traits> )
| |
no-op (public member function of std::ostreambuf_iterator<CharT,Traits> )
| |
advances the iterator to the next match (public member function of std::regex_iterator<BidirIt,CharT,Traits> )
| |
advances the iterator to the next submatch (public member function of std::regex_token_iterator<BidirIt,CharT,Traits> )
|
See also
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement |
arithmetic | logical | comparison | member access |
other |
a = b |
++a |
+a |
!a |
a == b |
a[...] |
function call |
a(...) | ||||||
comma | ||||||
a, b | ||||||
conditional | ||||||
a ? b : c | ||||||
Special operators | ||||||
static_cast converts one type to another related type |