Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/operator incdec"

From cppreference.com
< cpp‎ | language
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
(do not link through a redirect)
Line 118: Line 118:
 
{{dcl list h2 | overloads for iterator types}}
 
{{dcl list h2 | overloads for iterator types}}
 
{{dcl list mem fun | cpp/memory/raw_storage_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | advances the iterator}}
 
{{dcl list mem fun | cpp/memory/raw_storage_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | advances the iterator}}
{{dcl list mem fun | cpp/iterator/reverse_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | advances the iterator }}
+
{{dcl list mem fun | cpp/iterator/reverse_iterator/operator_arith | title=operator++<br>operator++{{dcl small|(int)}} | advances the iterator }}
{{dcl list mem fun | cpp/iterator/reverse_iterator/operator-- | title=operator--<br>operator++{{dcl small|(int)}} | decrements the iterator }}
+
{{dcl list mem fun | cpp/iterator/reverse_iterator/operator_arith | title=operator--<br>operator++{{dcl small|(int)}} | decrements the iterator }}
 
{{dcl list mem fun | cpp/iterator/back_insert_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | no-op }}
 
{{dcl list mem fun | cpp/iterator/back_insert_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | no-op }}
 
{{dcl list mem fun | cpp/iterator/front_insert_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | no-op }}
 
{{dcl list mem fun | cpp/iterator/front_insert_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | no-op }}

Revision as of 08:29, 10 May 2013

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

Increment/decrement operators increments or decrements the value of the object.

Operator name Syntax Over​load​able 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);
Notes
  • Prefix forms of the built-in operators return references and postfix forms return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
  • The int parameter is a dummy parameter used to differentiate between pre- and post- in versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation, e.g. a.operator++(2).

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: Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
A& operator++(A&)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
bool& operator++(bool&)
</td>

<td class="t-dcl-nopad"> </td> <td > (deprecated) </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
P& operator++(P&)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
A& operator--(A&)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
P& operator--(P&)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The operand of a built-in prefix increment or decrement operator must be a modifiable lvalue (non-const reference) 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: Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
A operator++(A&, int)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
bool operator++(bool&, int)
</td>

<td class="t-dcl-nopad"> </td> <td > (deprecated) </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
P operator++(P&, int)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
A operator--(A&, int)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
P operator--(P&, int)
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The operand of a built-in postfix increment or decrement operator must be a modifiable lvalue (non-const reference) 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.

Template:cpp/atomic/atomic/dcl list operator arith
overloads for arithmetic types
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 the iterator
(public member function of std::reverse_iterator<Iter>)
decrements the iterator
(public member function of std::reverse_iterator<Iter>)
no-op
(public member function of std::back_insert_iterator<Container>)
no-op
(public member function of std::front_insert_iterator<Container>)
no-op
(public member function of std::insert_iterator<Container>)
advances the iterator
(public member function of std::move_iterator<Iter>)
decrements the iterator
(public member function of std::move_iterator<Iter>)
advances the istream_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 istreambuf_iterator
(public member function of std::istreambuf_iterator<CharT,Traits>)
no-op
(public member function of std::ostreambuf_iterator<CharT,Traits>)
advances the regex_iterator
(public member function of std::regex_iterator<BidirIt,CharT,Traits>)
advances the regex_token_iterator
(public member function of std::regex_token_iterator<BidirIt,CharT,Traits>)

See also

Operator precedence

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = 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

function call
a(...)
comma
a, b
conditional
a ? b : c
Special operators

static_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 parameter 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)