Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
m (http -> https, fmt)
(Added CWG issue #2853 DR.)
 
(17 intermediate revisions by 6 users not shown)
Line 3: Line 3:
 
Returns the result of specific arithmetic operation.
 
Returns the result of specific arithmetic operation.
  
{|class="wikitable" style="font-size:85%;"
+
{|class="wikitable"<!--style="font-size:85%;"-->
 
|-
 
|-
 
!rowspan="2"|Operator name
 
!rowspan="2"|Operator name
 
!rowspan="2"|Syntax
 
!rowspan="2"|Syntax
!rowspan="2"|{{rlp|operators|Over&#8203;load&#8203;able}}
 
 
!colspan="2"|Prototype examples (for {{c/core|class T}})
 
!colspan="2"|Prototype examples (for {{c/core|class T}})
 
|-
 
|-
Line 13: Line 12:
 
!Outside class definition
 
!Outside class definition
 
|-
 
|-
|unary plus
+
|Unary plus
|{{tt|+a}}
+
|{{co|+a}}
|{{yes}}
+
 
|{{c|T T::operator+() const;}}
 
|{{c|T T::operator+() const;}}
|{{c|T operator+(const T &a);}}
+
|{{c|T operator+(const T& a);}}
 
|-
 
|-
|unary minus
+
|Unary minus
|{{tt|-a}}
+
|{{co|-a}}
|{{yes}}
+
 
|{{c|T T::operator-() const;}}
 
|{{c|T T::operator-() const;}}
|{{c|T operator-(const T &a);}}
+
|{{c|T operator-(const T& a);}}
 
|-
 
|-
|addition
+
|Addition
|{{tt|a + b}}
+
|{{co|a + b}}
|{{yes}}
+
|{{c|T T::operator+(const T2& b) const;}}
|{{c|T T::operator+(const T2 &b) const;}}
+
|{{c|T operator+(const T& a, const T2& b);}}
|{{c|T operator+(const T &a, const T2 &b);}}
+
 
|-
 
|-
|subtraction
+
|Subtraction
|{{tt|a - b}}
+
|{{co|a - b}}
|{{yes}}
+
|{{c|T T::operator-(const T2& b) const;}}
|{{c|T T::operator-(const T2 &b) const;}}
+
|{{c|T operator-(const T& a, const T2& b);}}
|{{c|T operator-(const T &a, const T2 &b);}}
+
 
|-
 
|-
|multiplication
+
|Multiplication
|{{tt|a * b}}
+
|{{co|a * b}}
|{{yes}}
+
|{{c|T T::operator*(const T2& b) const;}}
|{{c|T T::operator*(const T2 &b) const;}}
+
|{{c|T operator*(const T& a, const T2& b);}}
|{{c|T operator*(const T &a, const T2 &b);}}
+
 
|-
 
|-
|division
+
|Division
|{{tt|a / b}}
+
|{{co|a / b}}
|{{yes}}
+
|{{c|T T::operator/(const T2& b) const;}}
|{{c|T T::operator/(const T2 &b) const;}}
+
|{{c|T operator/(const T& a, const T2& b);}}
|{{c|T operator/(const T &a, const T2 &b);}}
+
 
|-
 
|-
|remainder
+
|Remainder
|{{tt|a % b}}
+
|{{co|a % b}}
|{{yes}}
+
|{{c|T T::operator%(const T2& b) const;}}
|{{c|T T::operator%(const T2 &b) const;}}
+
|{{c|T operator%(const T& a, const T2& b);}}
|{{c|T operator%(const T &a, const T2 &b);}}
+
 
|-
 
|-
|bitwise NOT
+
|Bitwise NOT
|{{tt|~a}}
+
|{{co|~a}}
|{{yes}}
+
 
|{{c|T T::operator~() const;}}
 
|{{c|T T::operator~() const;}}
|{{c|T operator~(const T &a);}}
+
|{{c|T operator~(const T& a);}}
 
|-
 
|-
|bitwise AND  
+
|Bitwise AND
|{{tt|a & b}}
+
|{{co|a & b}}
|{{yes}}
+
|{{c|T T::operator&(const T2& b) const;}}
|{{c|T T::operator&(const T2 &b) const;}}
+
|{{c|T operator&(const T& a, const T2& b);}}
|{{c|T operator&(const T &a, const T2 &b);}}
+
 
|-
 
|-
|bitwise OR  
+
|Bitwise OR
|{{tt|a &#124; b}}
+
|{{co|a {{!}} b}}
|{{yes}}
+
|{{c|T T::operator{{!}}(const T2& b) const;}}
|{{c|T T::operator{{!}}(const T2 &b) const;}}
+
|{{c|T operator{{!}}(const T& a, const T2& b);}}
|{{c|T operator{{!}}(const T &a, const T2 &b);}}
+
 
|-
 
|-
|bitwise XOR  
+
|Bitwise XOR
|{{tt|a ^ b}}
+
|{{co|a ^ b}}
|{{yes}}
+
|{{c|T T::operator^(const T2& b) const;}}
|{{c|T T::operator^(const T2 &b) const;}}
+
|{{c|T operator^(const T& a, const T2& b);}}
|{{c|T operator^(const T &a, const T2 &b);}}
+
 
|-
 
|-
|bitwise left shift  
+
|Bitwise left shift
|{{tt|a << b}}
+
|{{co|a << b}}
|{{yes}}
+
|{{c|T T::operator<<(const T2& b) const;}}
|{{c|T T::operator<<(const T2 &b) const;}}
+
|{{c|T operator<<(const T& a, const T2& b);}}
|{{c|T operator<<(const T &a, const T2 &b);}}
+
 
|-
 
|-
|bitwise right shift  
+
|Bitwise right shift
|{{tt|a >> b}}
+
|{{co|a >> b}}
|{{yes}}
+
|{{c|T T::operator>>(const T2& b) const;}}
|{{c|T T::operator>>(const T2 &b) const;}}
+
|{{c|T operator>>(const T& a, const T2& b);}}
|{{c|T operator>>(const T &a, const T2 &b);}}
+
 
|-
 
|-
 
|colspan="5"|
 
|colspan="5"|
 
:'''Notes'''<br>
 
:'''Notes'''<br>
 +
* All operators in this table are {{rlp|operators|overloadable}}.
 
* All built-in operators return values, and most {{rlp|operators|user-defined overloads}} also return values 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 {{c/core|void}}). In particular, stream insertion and stream extraction overloads of {{c/core|operator<<}} and {{c/core|operator>>}} return {{tt|T&}}.
 
* All built-in operators return values, and most {{rlp|operators|user-defined overloads}} also return values 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 {{c/core|void}}). In particular, stream insertion and stream extraction overloads of {{c/core|operator<<}} and {{c/core|operator>>}} return {{tt|T&}}.
 
* {{tt|T2}} can be any type including {{tt|T}}.
 
* {{tt|T2}} can be any type including {{tt|T}}.
 
|}
 
|}
  
===Explanation===
+
===General explanation===
All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.
+
All built-in arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.
  
 
====Conversions====
 
====Conversions====
If the operand passed to an arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes {{rlp|implicit conversion#Integral promotion|integral promotion}}. If an operand has array or function type, {{rlp|implicit conversion#array to pointer conversion|array-to-pointer}} and {{rlp|implicit conversion#function to pointer|function-to-pointer}} conversions are applied.
+
If the operand passed to a built-in arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes {{rlpsd|implicit conversion#Integral promotion}}. If an operand has array or function type, {{rlp|implicit conversion#Array-to-pointer conversion|array-to-pointer}} and {{rlpsd|implicit conversion#Function-to-pointer}} conversions are applied.
  
 
For the binary operators (except shifts), if the promoted operands have different types, {{rlp|usual arithmetic conversions}} are applied.
 
For the binary operators (except shifts), if the promoted operands have different types, {{rlp|usual arithmetic conversions}} are applied.
Line 109: Line 96:
  
 
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include:
 
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include:
* it wraps around according to the rules of the representation (typically 2's complement),
+
* it wraps around according to the rules of the representation (typically {{rlp|types#Range of values|two's complement}}),
 
* it traps — on some platforms or due to compiler options (e.g. {{tt|-ftrapv}} in GCC and Clang),
 
* it traps — on some platforms or due to compiler options (e.g. {{tt|-ftrapv}} in GCC and Clang),
 
* it saturates to minimal or maximal value (on many DSPs),
 
* it saturates to minimal or maximal value (on many DSPs),
Line 124: Line 111:
 
Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations.
 
Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations.
  
====Unary arithmetic operators====
+
===Unary arithmetic operators===
The unary arithmetic operator expressions have the form  
+
The unary arithmetic operator expressions have the form
  
 
{{sdsc begin}}
 
{{sdsc begin}}
Line 131: Line 118:
 
{{sdsc|num=2|{{ttb|-}} {{spar|expression}}}}
 
{{sdsc|num=2|{{ttb|-}} {{spar|expression}}}}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ unary plus (promotion).
+
@1@ Unary plus (promotion).
@@ For the built-in operator, {{spar|expression}} must have arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on the operand if it has integral or unscoped enumeration type and determines the type of the result.
+
@2@ Unary minus (negation).
@2@ unary minus (negation).
+
@@ For the built-in operator, {{spar|expression}} must have arithmetic or unscoped enumeration type. Integral promotion is performed on the operand and determines the type of the result.
+
  
The built-in unary plus operator returns the value of its operand. The only situation where it is not a no-op is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, e.g, it converts {{c/core|char}} to {{c/core|int}} or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.
+
Unary {{tt|+}} and {{tt|-}} operators have higher {{rlp|operator precedence|precedence}} than all binary arithmetic operators, so {{spar|expression}} cannot contain top-level binary arithmetic operators. These operators associate from right to left:
 +
{{source|
 +
+a - b; // equivalent to (+a) - b, NOT +(a - b)
 +
-c + d; // equivalent to (-c) + d, NOT -(c + d)
  
The builtin unary minus operator calculates the negative of its promoted operand. For unsigned {{tt|a}}, the value of {{tt|-a}} is {{math|2{{su|p=b}}-a}}, where {{tt|b}} is the number of bits after promotion.
+
+-e; // equivalent to +(-e), the unary + is a no-op if “e” is a built-in type
 +
    // because any possible promotion is performed during negation already
 +
}}
  
 +
====Built-in unary arithmetic operators====
 +
@1@ For the built-in unary plus operator, {{spar|expression}} must be a prvalue of arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on {{spar|expression}} if it has integral or unscoped enumeration type. The type of the result is the (possibly promoted) type of {{spar|expression}}.
 +
@@ The result of the built-in promotion is the value of {{spar|expression}}. The built-in unary operation is no-op if the operand is a prvalue of a promoted integral type or a pointer type. Otherwise, the type or value category of the operand is changed by integral promotion or lvalue-to-rvalue, array-to-pointer, function-to-pointer, or user-defined conversion. For example, {{c/core|char}} is converted to {{c/core|int}} {{rev inl|since=c++11|, and non-generic captureless {{rlp|lambda|lambda expression}} is converted to function pointer}} in unary plus expressions.
 +
 +
@2@ For the built-in unary minus operator, {{spar|expression}} must be a prvalue of arithmetic or unscoped enumeration type. Integral promotion is performed on {{spar|expression}}. The type of the result is the type of the promoted type of {{spar|expression}}.
 +
@@ The result of the built-in negation is the negative of the promoted {{spar|expression}}. For unsigned {{c|a}}, the value of {{c|-a}} is {{mathjax-or|\({\small 2^N-a}\)|2{{su|p=N}}-a}}, where {{c|N}} is the number of bits after promotion.
 +
* In other words, the result is the two’s complement of the operand (where operand and result are considered as unsigned).
 +
 +
{{anchor|Overloads of unary arithmetic operators}}
 +
 +
====Overloads====
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every cv-unqualified promoted arithmetic type {{tt|A}} and for every type {{tt|T}}, the following function signatures participate in overload resolution:
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every cv-unqualified promoted arithmetic type {{tt|A}} and for every type {{tt|T}}, the following function signatures participate in overload resolution:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl|A operator+(A)}}
 
{{dcl|A operator+(A)}}
 
{{dcl|T* operator+(T*)}}
 
{{dcl|T* operator+(T*)}}
{{dcl|A operator-(A)}}  
+
{{dcl|A operator-(A)}}
 
{{dcl end}}
 
{{dcl end}}
  
 
{{example
 
{{example
|p=true
 
 
|code=
 
|code=
 
#include <iostream>
 
#include <iostream>
Line 158: Line 158:
 
     unsigned char n2 = 1;
 
     unsigned char n2 = 1;
 
     unsigned int n3 = 1;
 
     unsigned int n3 = 1;
     std::cout << "char: " << c << " int: " << +c << '\n'
+
     std::cout << "char: " << c << " int: " << +c << "\n"
              << "-1, where 1 is signed: " << -n1 << '\n'
+
                "-1, where 1 is signed: " << -n1 << "\n"
              << "-1, where 1 is unsigned char: " << -n2 << '\n'
+
                "-1, where 1 is unsigned char: " << -n2 << "\n"
              << "-1, where 1 is unsigned int: " << -n3 << '\n';
+
                "-1, where 1 is unsigned int: " << -n3 << '\n';
 
     char a[3];
 
     char a[3];
     std::cout << "size of array: " << sizeof a << '\n'
+
     std::cout << "size of array: " << sizeof a << "\n"
              << "size of pointer: " << sizeof +a << '\n';
+
                "size of pointer: " << sizeof +a << '\n';
 
}
 
}
 +
|p=true
 
|output=
 
|output=
 
char: j int: 106
 
char: j int: 106
Line 175: Line 176:
 
}}
 
}}
  
====Additive operators====
+
===Additive operators===
The binary additive arithmetic operator expressions have the form  
+
The additive operator expressions have the form
  
 
{{sdsc begin}}
 
{{sdsc begin}}
Line 182: Line 183:
 
{{sdsc|num=2|{{spar|lhs}} {{ttb|-}} {{spar|rhs}}}}
 
{{sdsc|num=2|{{spar|lhs}} {{ttb|-}} {{spar|rhs}}}}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ addition
+
@1@ Binary plus (addition).
@@ For the built-in operator, {{spar|lhs}} and {{spar|rhs}} must be one of the following:
+
@2@ Binary minus (subtraction).
:* both have arithmetic or unscoped enumeration type. In this case, the {{rlp|usual arithmetic conversions}} are performed on both operands and determine the type of the result.
+
:* one is a pointer to completely-defined object type, the other has integral or unscoped enumeration type. In this case, the result type has the type of the pointer.
+
@2@ subtraction
+
@@ For the built-in operator, {{spar|lhs}} and {{spar|rhs}} must be one of the following:
+
:* both have arithmetic or unscoped enumeration type. In this case, the {{rlp|usual arithmetic conversions}} are performed on both operands and determine the type of the result.
+
:* {{spar|lhs}} is a pointer to completely-defined object type, {{spar|rhs}} has integral or unscoped enumeration type. In this case, the result type has the type of the pointer.
+
:* both are pointers to the same completely-defined object types, ignoring cv-qualifiers. In this case, the result type is {{lc|std::ptrdiff_t}}.
+
  
With operands of arithmetic or enumeration type, the result of binary plus is the sum of the operands (after {{rlp|usual arithmetic conversions}}), and the result of the binary minus operator is the result of subtracting the second operand from the first (after {{rlp|usual arithmetic conversions}}), except that, if the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}),
+
Binary {{tt|+}} and {{tt|-}} operators have higher {{rlp|operator precedence|precedence}} than all other binary arithmetic operators except {{tt|*}}, {{tt|/}} and {{tt|%}}. These operators associate from left to right:
:* if one operand is NaN, the result is NaN
+
{{source|
:* infinity minus infinity is NaN and {{lc|FE_INVALID}} is raised
+
a + b * c;  // equivalent to a + (b * c),  NOT (a + b) * c
:* infinity plus the negative infinity is NaN and {{lc|FE_INVALID}} is raised
+
d / e - f;  // equivalent to (d / e) - f,  NOT d / (e - f)
 +
g + h >> i; // equivalent to (g + h) >> i, NOT g + (h >> i)
 +
 
 +
j - k + l - m; // equivalent to ((j - k) + l) - m
 +
}}
 +
 
 +
====Built-in additive operators====
 +
For built-in binary plus and binary minus operators, both of {{spar|lhs}} and {{spar|rhs}} must be prvalues, and one of the following conditions must be satisfied:
 +
* Both operands have arithmetic or unscoped enumeration type. In this case, {{rlp|usual arithmetic conversions}} are performed on both operands.
 +
* Exactly one operand has integral or unscoped enumeration type. In this case, integral promotion is applied to that operand.
 +
 
 +
In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s).
 +
 
 +
@1@ For built-in addition, one of the following conditions must be satisfied:
 +
* Both operands have arithmetic type. In this case, the result is the sum of the operands.
 +
* One operand is a pointer to a completely-defined object type, and the other operand has integral type. In this case, the integral value is added to the pointer (see {{lsd|#Pointer arithmetic}}).
 +
@2@ For built-in subtraction, one of the following conditions must be satisfied:
 +
* Both operands have arithmetic type. In this case, the result is the difference resulting from the subtraction of {{spar|rhs}} from {{spar|lhs}}.
 +
* {{spar|lhs}} is a pointer to a completely-defined object type, and {{spar|rhs}} has integral type. In this case, the integral value is subtracted from the pointer (see {{lsd|#Pointer arithmetic}}).
 +
* Both operands are pointers to cv-qualified or cv-unqualified versions of the same completely-defined object type. In this case {{spar|rhs}} is subtracted from {{spar|lhs}} (see {{lsd|#Pointer arithmetic}}).
 +
 
 +
If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}):
 +
* If one operand is NaN, the result is NaN.
 +
* Infinity minus infinity is NaN, and {{lc|FE_INVALID}} is raised.
 +
* Infinity plus the negative infinity is NaN, and {{lc|FE_INVALID}} is raised.
 +
 
 +
====Pointer arithmetic====
 +
When an expression {{c|J}} that has integral type is added to or subtracted from an expression {{c|P}} of pointer type, the result has the type of {{c|P}}.
 +
* If {{c|P}} evaluates to a {{rlp|pointer#Null pointers|null pointer value}} and {{c|J}} evaluates to {{c|0}}, the result is a null pointer value.
 +
* Otherwise, if {{c|P}} points to the {{tt|i}}th element of an array object {{c|x}} with {{c|n}} elements, given the value of {{c|J}} as {{c|j}}, {{c|P}} is added or subtracted as follows:
 +
:* The expressions {{c|P + J}} and {{c|J + P}}
 +
::* point to the {{tt|i+j}}th element of {{c|x}} if {{c|i + j}} is in {{range|0|n}}, and
 +
::* are pointers past the end of the last element of {{c|x}} if {{c|i + j}} is {{c|n}}.
 +
:* The expression {{c|P - J}}
 +
::* points to the {{tt|i-j}}th element of {{c|x}} if {{c|i - j}} is in {{range|0|n}}, and
 +
::* is a pointer past the end of the last element of {{c|x}} if {{c|i - j}} is {{c|n}}.
 +
:* Other {{c|j}} values result in undefined behavior.
 +
* Otherwise, if {{c|P}} points to a complete object, a base class subobject or a member subobject {{c|y}}, given the value of {{c|J}} as {{c|j}}, {{c|P}} is added or subtracted as follows:
 +
:* The expressions {{c|P + J}} and {{c|J + P}}
 +
::* point to {{c|y}} if {{c|j}} is {{c|0}}, and
 +
::* are pointers past the end of {{c|y}} if {{c|j}} is {{c|1}}.
 +
:* The expression {{c|P - J}}
 +
::* points to {{c|y}} if {{c|j}} is {{c|0}}, and
 +
::* is a pointer past the end of {{c|y}} if {{c|j}} is {{c|-1}}.
 +
:* Other {{c|j}} values result in undefined behavior.
 +
* Otherwise, if {{c|P}} is a pointer past the end of an object {{c|z}}, given the value of {{c|J}} as {{c|j}}:
 +
:* If {{c|z}} is an array object with {{c|n}} elements, {{c|P}} is added or subtracted as follows:
 +
::* The expressions {{c|P + J}} and {{c|J + P}}
 +
:::* point to the {{tt|n+j}}th element of {{c|z}} if {{c|n + j}} is in {{range|0|n}}, and
 +
:::* are pointers past the end of the last element of {{c|z}} if {{c|j}} is {{c|0}}.
 +
::* The expression {{c|P - J}}
 +
:::* points to the {{tt|n-j}}th element of {{c|z}} if {{c|n - j}} is in {{range|0|n}}, and
 +
:::* is a pointer past the end of the last element of {{c|z}} if {{c|j}} is {{c|0}}.
 +
::* Other {{c|j}} values result in undefined behavior.
 +
:* Otherwise, {{c|P}} is added or subtracted as follows:
 +
::* The expressions {{c|P + J}} and {{c|J + P}}
 +
:::* point to {{c|z}} if {{c|j}} is {{c|-1}}, and
 +
:::* are pointers past the end of {{c|z}} if {{c|j}} is {{c|0}}.
 +
::* The expression {{c|P - J}}
 +
:::* points to {{c|z}} if {{c|j}} is {{c|1}}, and
 +
:::* is a pointer past the end of {{c|z}} if {{c|j}} is {{c|0}}.
 +
::* Other {{c|j}} values result in undefined behavior.
 +
* Otherwise, the behavior is undefined.
 +
 
 +
When two pointer expressions {{c|P}} and {{c|Q}} are subtracted, the type of the result is  {{lc|std::ptrdiff_t}}.
 +
* If {{c|P}} and {{c|Q}} both evaluate to {{rlp|pointer#Null pointers|null pointer values}}, the result is {{c|0}}.
 +
* Otherwise, if {{c|P}} and {{c|Q}} point to, respectively, the {{tt|i}}th and {{tt|j}}th array elements of the same array object {{c|x}}, the expression {{c|P - Q}} has the value {{c|i − j}}.
 +
:* If {{c|i − j}} is not representable by {{lc|std::ptrdiff_t}}, the behavior is undefined.
 +
* Otherwise, if {{c|P}} and {{c|Q}} point to the same complete object, base class subobject or member subobject, the result is {{c|0}}.
 +
* Otherwise, the behavior is undefined.
  
If any of the operands is a pointer, the following rules apply:
 
* A pointer to non-array object is treated as a pointer to the first element of an array with size 1.
 
* If the pointer {{c|P}} points to the {{tt|i}}th element of an array, then the expressions {{c|P + n}}, {{c|n + P}}, and {{c|P - n}} are pointers of the same type that point to the {{tt|i+n}}th, {{tt|i+n}}th, and {{tt|i-n}}th element of the same array, respectively. The result of pointer addition may also be a one-past-the-end pointer (that is, pointer {{c|P}} such that the expression {{c|P - 1}} points to the last element of the array). Any other situations (that is, attempts to generate a pointer that isn't pointing at an element of the same array or one past the end) invoke undefined behavior.
 
* If the pointer {{c|P}} points to the {{tt|i}}th element of an array, and the pointer {{c|Q}} points at the {{tt|j}}th element of the same array, the expression {{c|P - Q}} has the value {{c|i - j}}, if the value fits in {{lc|std::ptrdiff_t}}. Both operands must point to the elements of the same array (or one past the end), otherwise the behavior is undefined. If the result does not fit in {{lc|std::ptrdiff_t}}, the behavior is undefined.
 
* In any case, if the pointed-to type is different from the array element type, disregarding cv-qualifications, at every level if the elements are themselves pointers, the behavior of pointer arithmetic is undefined. In particular, pointer arithmetic with pointer to base, which is pointing at an element of an array of derived objects is undefined.
 
* If the value {{c|0}} is added or subtracted from a pointer, the result is the pointer, unchanged. If two pointers point at the same object or are both one past the end of the same array, or both are null pointers, then the result of subtraction is equal to {{c|(std::ptrdiff_t)0}}.
 
 
These pointer arithmetic operators allow pointers to satisfy the {{named req|RandomAccessIterator}} requirements.
 
These pointer arithmetic operators allow pointers to satisfy the {{named req|RandomAccessIterator}} requirements.
  
 +
For addition and subtraction, if {{c|P}} or {{c|Q}} have type “pointer to (possibly cv-qualified) {{tt|T}}”, where {{tt|T}} and the array element type are not {{rlp|implicit conversion#Similar types|similar}}, the behavior is undefined:
 +
{{source|1=
 +
int arr[5] = {1, 2, 3, 4, 5};
 +
unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1);
 +
unsigned int k = *p; // OK, the value of “k” is 2
 +
unsigned int *q = p + 1; // undefined behavior: “p” points to int, not unsigned int
 +
}}
 +
 +
{{anchor|Overloads of additive operators}}
 +
====Overloads====
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|L}} and {{tt|R}} and for every object type {{tt|T}}, the following function signatures participate in overload resolution:
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|L}} and {{tt|R}} and for every object type {{tt|T}}, the following function signatures participate in overload resolution:
 
{{dcl begin}}
 
{{dcl begin}}
Line 225: Line 292:
 
     unsigned int un = 2;
 
     unsigned int un = 2;
 
     int n = -10;
 
     int n = -10;
     std::cout << " 2 + (-10), where 2 is a char    = " << c + n << '\n'
+
     std::cout << " 2 + (-10), where 2 is a char    = " << c + n << "\n"
              << " 2 + (-10), where 2 is unsigned  = " << un + n << '\n'
+
                " 2 + (-10), where 2 is unsigned  = " << un + n << "\n"
              << " -10 - 2.12  = " << n - 2.12 << '\n';
+
                " -10 - 2.12  = " << n - 2.12 << '\n';
  
 
     char a[4] = {'a', 'b', 'c', 'd'};
 
     char a[4] = {'a', 'b', 'c', 'd'};
Line 245: Line 312:
  
 
====Multiplicative operators====
 
====Multiplicative operators====
The binary multiplicative arithmetic operator expressions have the form  
+
The multiplicative operator expressions have the form
  
 
{{sdsc begin}}
 
{{sdsc begin}}
Line 252: Line 319:
 
{{sdsc|num=3|{{spar|lhs}} {{ttb|%}} {{spar|rhs}}}}
 
{{sdsc|num=3|{{spar|lhs}} {{ttb|%}} {{spar|rhs}}}}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ multiplication
+
@1@ Multiplication.
@@ For the built-in operator, {{spar|lhs}} and {{spar|rhs}} must both have arithmetic or unscoped enumeration type.  
+
@2@ Division.
@2@ division
+
@3@ Remainder.
@@ For the built-in operator, {{spar|lhs}} and {{spar|rhs}} must both have arithmetic or unscoped enumeration type.  
+
@3@ remainder
+
@@ For the built-in operator, {{spar|lhs}} and {{spar|rhs}} must both have integral or unscoped enumeration type.
+
  
For all three operators, the {{rlp|usual arithmetic conversions}} are performed on both operands and determine the type of the result.
+
Multiplicative operators have higher {{rlp|operator precedence|precedence}} than all other binary arithmetic operators. These operators associate from left to right:
 +
{{source|
 +
a + b * c;  // equivalent to a + (b * c),  NOT (a + b) * c
 +
d / e - f;  // equivalent to (d / e) - f,  NOT d / (e - f)
 +
g % h >> i; // equivalent to (g % h) >> i, NOT g % (h >> i)
  
The binary operator * performs multiplication of its operands (after {{rlp|usual arithmetic conversions}}), except that, for floating-point multiplication,
+
j * k / l % m; // equivalent to ((j * k) / l) % m
* multiplication of a NaN by any number gives NaN
+
}}
* multiplication of infinity by zero gives NaN and {{lc|FE_INVALID}} is raised
+
 
 +
====Built-in multiplicative operators====
 +
For built-in multiplication and division operators, both operands must have arithmetic or unscoped enumeration type. For the built-in remainder operator, both operands must have integral or unscoped enumeration type. {{rlp|usual arithmetic conversions|Usual arithmetic conversions}} are performed on both operands.
  
The binary operator / divides the first operand by the second (after {{rlp|usual arithmetic conversions}}).
+
In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted operand(s).
  
For integral operands, it yields the algebraic quotient.
+
@1@ The result of built-in multiplication is the product of the operands.
The quotient is truncated towards zero (fractional part is discarded).
+
@@ If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}):
 +
* Multiplication of a NaN by any number gives NaN.
 +
* Multiplication of infinity by zero gives NaN and {{lc|FE_INVALID}} is raised.
  
If the second operand is zero, the behavior is undefined, except that if floating-point division is taking place and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}), then:
+
@2@ The result of built-in division is {{spar|lhs}} divided by {{spar|rhs}}. If {{spar|rhs}} is zero, the behavior is undefined.
* if one operand is NaN, the result is NaN
+
@@ If both operands have an integral type, the result is the algebraic quotient (performs integer division): the quotient is truncated towards zero (fractional part is discarded).
* dividing a non-zero number by ±0.0 gives the correctly-signed infinity and {{lc|FE_DIVBYZERO}} is raised
+
@@ If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}):
* dividing 0.0 by 0.0 gives NaN and {{lc|FE_INVALID}} is raised
+
* If one operand is NaN, the result is NaN.
 +
* Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and {{lc|FE_DIVBYZERO}} is raised.
 +
* Dividing 0.0 by 0.0 gives NaN and {{lc|FE_INVALID}} is raised.
  
The binary operator % yields the remainder of the integer division of the first operand by the second (after {{rlp|usual arithmetic conversions}}; note that the operand types must be integral types). If the quotient {{c|a / b}} is representable in the result type, {{c|1=(a / b) * b + a % b == a}}. If the second operand is zero, the behavior is undefined. If the quotient {{c|a / b}} is not representable in the result type, the behavior of both {{c|a / b}} and {{c|a % b}} is undefined (that means {{c|INT_MIN % -1}} is undefined on 2's complement systems).
+
@3@ The result of built-in remainder is the remainder of the integer division of {{spar|lhs}} by {{spar|rhs}}. If {{spar|rhs}} is zero, the behavior is undefined.
 +
@@ If {{c|a / b}} is representable in the result type, {{c|1=(a / b) * b + a % b == a}}.
 +
@@ If {{c|a / b}} is not representable in the result type, the behavior of both {{c|a / b}} and {{c|a % b}} is undefined (that means {{c|INT_MIN % -1}} is undefined on two's complement systems).
  
 
Note: Until {{cwg|614}} was resolved ({{stddoc|n2757}}), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function {{lc|std::div}} provided well-defined behavior in that case.
 
Note: Until {{cwg|614}} was resolved ({{stddoc|n2757}}), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function {{lc|std::div}} provided well-defined behavior in that case.
Line 281: Line 357:
 
Note: for floating-point remainder, see {{lc|std::remainder}} and {{lc|std::fmod}}.
 
Note: for floating-point remainder, see {{lc|std::remainder}} and {{lc|std::fmod}}.
  
 +
{{anchor|Overloads of multiplicative operators}}
 +
====Overloads====
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|LA}} and {{tt|RA}} and for every pair of promoted integral types {{tt|LI}} and {{tt|RI}} the following function signatures participate in overload resolution:
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|LA}} and {{tt|RA}} and for every pair of promoted integral types {{tt|LI}} and {{tt|RI}} the following function signatures participate in overload resolution:
 
{{dcl begin}}
 
{{dcl begin}}
Line 298: Line 376:
 
     unsigned int un = 2;
 
     unsigned int un = 2;
 
     int  n = -10;
 
     int  n = -10;
     std::cout << "2 * (-10), where 2 is a char    = " << c * n << '\n'
+
     std::cout << "2 * (-10), where 2 is a char    = " << c * n << "\n"
              << "2 * (-10), where 2 is unsigned  = " << un * n << '\n'
+
                "2 * (-10), where 2 is unsigned  = " << un * n << "\n"
              << "-10 / 2.12  = " << n / 2.12 << '\n'
+
                "-10 / 2.12  = " << n / 2.12 << "\n"
              << "-10 / 21  = " << n / 21 << '\n'
+
                "-10 / 21  = " << n / 21 << "\n"
              << "-10 % 21  = " << n % 21 << '\n';
+
                "-10 % 21  = " << n % 21 << '\n';
 
}
 
}
 
|output=
 
|output=
Line 312: Line 390:
 
}}
 
}}
  
====Bitwise logic operators====
+
===Bitwise logic operators===
The bitwise arithmetic operator expressions have the form  
+
The bitwise logic operator expressions have the form
  
 
{{sdsc begin}}
 
{{sdsc begin}}
Line 321: Line 399:
 
{{sdsc|num=4|{{spar|lhs}} {{ttb|^}} {{spar|rhs}}}}
 
{{sdsc|num=4|{{spar|lhs}} {{ttb|^}} {{spar|rhs}}}}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ bitwise NOT
+
@1@ Bitwise NOT.
@2@ bitwise AND
+
@2@ Bitwise AND.
@3@ bitwise OR
+
@3@ Bitwise OR.
@4@ bitwise XOR
+
@4@ Bitwise XOR.
@@ For the built-in operators, {{spar|lhs}} and {{spar|rhs}} must both have integral or unscoped enumeration type. {{rlp|usual arithmetic conversions|Usual arithmetic conversions}} are performed on both operands and determine the type of the result.
+
  
The result of operator~ is the bitwise NOT (all zero-bits become one-bits, all one-bits become zero-bits) value of the argument (after promotion). The result of operator& is the bitwise AND value of the operands (after {{rlp|usual arithmetic conversions}}). The result of operator| is the bitwise OR value of the operands (after {{rlp|usual arithmetic conversions}}). The result of operator^ is the bitwise XOR value of the operands (after {{rlp|usual arithmetic conversions}}).
+
The bitwise NOT operator has higher {{rlp|operator precedence|precedence}} than all binary arithmetic operators. It associates from right to left:
 +
{{source|
 +
~a - b; // equivalent to (~a) - b, NOT ~(a - b)
 +
~c * d; // equivalent to (~c) * d, NOT ~(c * d)
  
There is an ambiguity in the grammar when {{c|~}} is followed by a {{rlp|type#type naming|type name}} {{rev inl|since=c++11|or {{rlp|decltype}} specifier}}: it can either be operator~ or start a {{rlp|destructor}} identifier). The ambiguity is resolved by treating {{c|~}} as operator~. {{c|~}} can start a destructor identifier only in places where forming an operator~ is syntactically invalid.
+
~-e; // equivalent to ~(-e)
 +
}}
 +
 
 +
There is an ambiguity in the grammar when {{c|~}} is followed by a {{rlp|type#type naming|type name}}{{rev inl|since=c++11| or {{rlpt|decltype}} specifier}}: it can either be operator~ or start a {{rlp|destructor}} identifier). The ambiguity is resolved by treating {{c|~}} as operator~. {{c|~}} can start a destructor identifier only in places where forming an operator~ is syntactically invalid.
 +
 
 +
All other bitwise logic operators have lower {{rlp|operator precedence|precedence}} than all other binary arithmetic operators. Bitwise AND has higher precedence than bitwise XOR, which has higher precedence than bitwise OR. They associate from left to right:
 +
{{source|
 +
a & b * c;  // equivalent to a & (b * c),  NOT (a & b) * c
 +
d / e ^ f;  // equivalent to (d / e) ^ f,  NOT d / (e ^ f)
 +
g << h {{!}} i; // equivalent to (g << h) {{!}} i, NOT g << (h {{!}} i)
 +
 
 +
j & k & l; // equivalent to (j & k) & l
 +
m {{!}} n ^ o  // equivalent to m {{!}} (n ^ o)
 +
}}
 +
 
 +
====Built-in bitwise logic operators====
 +
For the built-in bitwise NOT operator, {{spar|rhs}} must be a prvalue of integral or unscoped enumeration type, and integral promotion is performed on {{spar|rhs}}. For other built-in bitwise logic operators, both operands must have integral or unscoped enumeration type, and {{rlp|usual arithmetic conversions}} are performed on both operands.
 +
 
 +
In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s).
 +
 
 +
@1@ Given the operand as {{c|x}} and the result of the built-in bitwise NOT operation as {{c|r}}. For each coefficient {{c|x_i}} of the base-2 representation of {{c|x}}, the corresponding coefficient {{c|r_i}} of the base-2 representation of {{c|r}} is {{c|1}} if {{c|x_i}} is {{c|0}}, and {{c|0}} otherwise.
 +
* In other words, the result is the one’s complement of the operand (where operand and result are considered as unsigned).
 +
@@ The type of the result {{c|r}} is the type of the operand {{c|x}}.
 +
 
 +
@2-4@ Given the operands as {{c|x}} and {{c|y}} respectively and the result of the built-in binary bitwise logic operations as {{c|r}}. For each pair of coefficients {{c|x_i}} and {{c|y_i}} of the base-2 representations of {{c|x}} and {{c|y}} respectively, the corresponding coefficient {{c|r_i}} of the base-2 representation of {{c|r}} is
 +
:@2@ {{c|1}} if both {{c|x_i}} and {{c|y_i}} are {{c|1}}, and {{c|0}} otherwise.
 +
:@3@ {{c|1}} if at least one of {{c|x_i}} and {{c|y_i}} is {{c|1}}, and {{c|0}} otherwise.
 +
:@4@ {{c|1}} if either (but not both) of {{c|x_i}} and {{c|y_i}} is {{c|1}}, and {{c|0}} otherwise.
 +
@@ The type of the result {{c|r}} is the type of the operands {{c|x}} and {{c|y}}.
  
 +
{{anchor|Overloads of bitwise logic operators}}
 +
====Overloads====
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}} the following function signatures participate in overload resolution:
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}} the following function signatures participate in overload resolution:
 
{{dcl begin}}
 
{{dcl begin}}
Line 343: Line 453:
 
|code=
 
|code=
 
#include <bitset>
 
#include <bitset>
 +
#include <cstdint>
 
#include <iomanip>
 
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
Line 348: Line 459:
 
int main()
 
int main()
 
{
 
{
     uint16_t mask = 0x00f0;
+
     std::uint16_t mask = 0x00f0;
     uint32_t x0 = 0x12345678;
+
     std::uint32_t x0 = 0x12345678;
     uint32_t x1 = x0 {{!}} mask;
+
     std::uint32_t x1 = x0 {{!}} mask;
     uint32_t x2 = x0 & ~mask;
+
     std::uint32_t x2 = x0 & ~mask;
     uint32_t x3 = x0 & mask;
+
     std::uint32_t x3 = x0 & mask;
     uint32_t x4 = x0 ^ mask;
+
     std::uint32_t x4 = x0 ^ mask;
     uint32_t x5 = ~x0;
+
     std::uint32_t x5 = ~x0;
 
     using bin16 = std::bitset<16>;
 
     using bin16 = std::bitset<16>;
 
     using bin32 = std::bitset<32>;
 
     using bin32 = std::bitset<32>;
 
     std::cout << std::hex << std::showbase
 
     std::cout << std::hex << std::showbase
               << "Mask: " << mask << std::setw(49) << bin16(mask) << '\n'
+
               << "Mask: " << mask << std::setw(49) << bin16(mask) << "\n"
              << "Value: " << x0 << std::setw(42) << bin32(x0) << '\n'
+
                "Value: " << x0 << std::setw(42) << bin32(x0) << "\n"
              << "Setting bits: " << x1 << std::setw(35) << bin32(x1) << '\n'
+
                "Setting bits: " << x1 << std::setw(35) << bin32(x1) << "\n"
              << "Clearing bits: " << x2 << std::setw(34) << bin32(x2) << '\n'
+
                "Clearing bits: " << x2 << std::setw(34) << bin32(x2) << "\n"
              << "Selecting bits: " << x3 << std::setw(39) << bin32(x3) << '\n'
+
                "Selecting bits: " << x3 << std::setw(39) << bin32(x3) << "\n"
              << "XOR-ing bits: " << x4 << std::setw(35) << bin32(x4) << '\n'
+
                "XOR-ing bits: " << x4 << std::setw(35) << bin32(x4) << "\n"
              << "Inverting bits: " << x5 << std::setw(33) << bin32(x5) << '\n';
+
                "Inverting bits: " << x5 << std::setw(33) << bin32(x5) << '\n';
 
}
 
}
 
|output=
 
|output=
Line 376: Line 487:
 
}}
 
}}
  
====Bitwise shift operators====
+
===Bitwise shift operators===
The bitwise shift operator expressions have the form  
+
The bitwise shift operator expressions have the form
  
 
{{sdsc begin}}
 
{{sdsc begin}}
Line 383: Line 494:
 
{{sdsc|num=2|{{spar|lhs}} {{ttb|>>}} {{spar|rhs}}}}
 
{{sdsc|num=2|{{spar|lhs}} {{ttb|>>}} {{spar|rhs}}}}
 
{{sdsc end}}
 
{{sdsc end}}
@1@ left shift of {{spar|lhs}} by {{spar|rhs}} bits
+
@1@ Bitwise left-shift.
@2@ right shift of {{spar|lhs}} by {{spar|rhs}} bits
+
@2@ Bitwise right-shift.
@@ For the built-in operators, {{spar|lhs}} and {{spar|rhs}} must both have integral or unscoped enumeration type. Integral promotions are performed on both operands.
+
 
 +
Bitwise shift operators have higher {{rlp|operator precedence|precedence}} than bitwise logic operators, but have lower precedence than additive and multiplicative operators. These operators associate from left to right:
 +
{{source|
 +
a >> b * c;  // equivalent to a >> (b * c),  NOT (a >> b) * c
 +
d << e & f;  // equivalent to (d << e) & f,  NOT d << (e & f)
 +
 
 +
g << h >> i; // equivalent to (g << h) >> i, NOT g << (h >> i)
 +
}}
 +
 
 +
====Built-in bitwise shift operators====
 +
For the built-in bitwise shift operators, both operands must be prvalues of integral or unscoped enumeration type. Integral promotions are performed on both operands.
 +
 
 +
In the remaining description in this section, "operand(s)", {{c|a}}, {{c|b}}, {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s).
  
The return type is the type of the left operand after integral promotions.  
+
If the value of {{spar|rhs}} is negative or is not less than the number of bits in {{spar|lhs}}, the behavior is undefined.
  
 
{{rrev multi|until1=c++20|rev1=
 
{{rrev multi|until1=c++20|rev1=
For unsigned {{c|a}}, the value of {{c|a << b}} is the value of {{math|a * 2{{su|p=b}}}}, reduced modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).  
+
For unsigned {{c|a}}, the value of {{c|a << b}} is the value of {{math|a * 2{{su|p=b}}}}, reduced modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).
  
 
For signed and non-negative {{c|a}}, if {{math|a * 2{{su|p=b}}}} is representable in the unsigned version of the return type, then that value, {{rlp|implicit conversion#Integral conversions|converted}} to signed, is the value of {{c|a << b}} (this makes it legal to create {{lc|INT_MIN}} as {{c|1 << 31}}); otherwise the behavior is undefined.
 
For signed and non-negative {{c|a}}, if {{math|a * 2{{su|p=b}}}} is representable in the unsigned version of the return type, then that value, {{rlp|implicit conversion#Integral conversions|converted}} to signed, is the value of {{c|a << b}} (this makes it legal to create {{lc|INT_MIN}} as {{c|1 << 31}}); otherwise the behavior is undefined.
  
For negative {{c|a}}, the behavior of {{c|a << b}} is undefined.  
+
For negative {{c|a}}, the behavior of {{c|a << b}} is undefined.
  
 
For unsigned {{c|a}} and for signed and non-negative {{c|a}}, the value of {{c|a >> b}} is the integer part of {{math|a/2{{su|p=b}}}}.
 
For unsigned {{c|a}} and for signed and non-negative {{c|a}}, the value of {{c|a >> b}} is the integer part of {{math|a/2{{su|p=b}}}}.
Line 402: Line 525:
 
The value of {{c|a << b}} is the unique value congruent to {{math|a * 2{{su|p=b}}}} modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).
 
The value of {{c|a << b}} is the unique value congruent to {{math|a * 2{{su|p=b}}}} modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).
  
The value of {{c|a >> b}} is {{math|a/2{{su|p=b}}}}, rounded down (in other words, right shift on signed {{c|a}} is arithmetic right shift).
+
The value of {{c|a >> b}} is {{math|a/2{{su|p=b}}}}, rounded towards negative infinity (in other words, right shift on signed {{c|a}} is arithmetic right shift).
 
}}
 
}}
  
In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.
+
The type of the result is that of {{spar|lhs}}.
  
 +
{{anchor|Overloads of bitwise shift operators}}
 +
====Overloads====
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}}, the following function signatures participate in overload resolution:
 
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}}, the following function signatures participate in overload resolution:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl|L operator<<(L, R)}}
 
{{dcl|L operator<<(L, R)}}
{{dcl|L operator>>(L, R)}}  
+
{{dcl|L operator>>(L, R)}}
 
{{dcl end}}
 
{{dcl end}}
  
Line 424: Line 549:
 
     char c = 0x10;
 
     char c = 0x10;
 
     unsigned long long ull = 0x123;
 
     unsigned long long ull = 0x123;
     std::cout << "0x123 << 1 = " << (ull << 1) << '\n'
+
     std::cout << "0x123 << 1 = " << (ull << 1) << "\n"
              << "0x123 << 63 = " << (ull << 63) << '\n' // overflow in unsigned
+
                "0x123 << 63 = " << (ull << 63) << "\n" // overflow in unsigned
              << "0x10 << 10 = " << (c << 10) << '\n';  // char is promoted to int
+
                "0x10 << 10 = " << (c << 10) << '\n';  // char is promoted to int
 
     long long ll = -1000;
 
     long long ll = -1000;
 
     std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n';
 
     std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n';
Line 442: Line 567:
 
====Unary arithmetic operators====
 
====Unary arithmetic operators====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/chrono/duration/dsc operator_arith}}
+
{{dsc inc|cpp/chrono/duration/dsc operator arith}}
{{dsc inc|cpp/numeric/complex/dsc operator_arith2}}
+
{{dsc inc|cpp/numeric/complex/dsc operator arith2}}
{{dsc inc|cpp/numeric/valarray/dsc operator_arith}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator arith}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Additive operators====
 
====Additive operators====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/chrono/time_point/dsc operator_arith2}}
+
{{dsc inc|cpp/chrono/time_point/dsc operator arith2}}
{{dsc inc|cpp/chrono/duration/dsc operator_arith4}}
+
{{dsc inc|cpp/chrono/duration/dsc operator arith4}}
 
{{dsc inc|cpp/chrono/year_month_day/dsc operator arith 2}}
 
{{dsc inc|cpp/chrono/year_month_day/dsc operator arith 2}}
 
{{dsc inc|cpp/string/basic_string/dsc operator+}}
 
{{dsc inc|cpp/string/basic_string/dsc operator+}}
{{dsc mem fun|cpp/iterator/reverse_iterator/operator_arith|title=operator+<br>operator-|advances or decrements the iterator}}
+
{{dsc mem fun|cpp/iterator/reverse_iterator/operator arith|title=operator+<br>operator-|advances or decrements the iterator}}
{{dsc mem fun|cpp/iterator/move_iterator/operator_arith|title=operator+<br/>operator-|advances or decrements the iterator}}
+
{{dsc mem fun|cpp/iterator/move_iterator/operator arith|title=operator+<br>operator-|advances or decrements the iterator}}
{{dsc inc|cpp/numeric/complex/dsc operator_arith3}}
+
{{dsc inc|cpp/numeric/complex/dsc operator arith3}}
{{dsc inc|cpp/numeric/valarray/dsc operator_arith3}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator arith3}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Multiplicative operators====
 
====Multiplicative operators====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/chrono/duration/dsc operator_arith4}}
+
{{dsc inc|cpp/chrono/duration/dsc operator arith4}}
{{dsc inc|cpp/numeric/complex/dsc operator_arith3}}
+
{{dsc inc|cpp/numeric/complex/dsc operator arith3}}
{{dsc inc|cpp/numeric/valarray/dsc operator_arith3}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator arith3}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Bitwise logic operators====
 
====Bitwise logic operators====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/utility/bitset/dsc operator_logic}}
+
{{dsc inc|cpp/utility/bitset/dsc operator logic}}
{{dsc inc|cpp/utility/bitset/dsc operator_logic2}}
+
{{dsc inc|cpp/utility/bitset/dsc operator logic2}}
{{dsc mem fun|cpp/numeric/valarray/operator_arith|title=operator~|applies a unary arithmetic operator to each element of the valarray}}
+
{{dsc mem fun|cpp/numeric/valarray/operator arith|title=operator~|applies a unary arithmetic operator to each element of the valarray}}
{{dsc tfun|cpp/numeric/valarray/operator_arith3|title=operator^<br>operator&<br>operator{{!}}|applies binary operators to each element of two valarrays, or a valarray and a value}}
+
{{dsc tfun|cpp/numeric/valarray/operator arith3|title=operator^<br>operator&<br>operator{{!}}|applies binary operators to each element of two valarrays, or a valarray and a value}}
 
{{dsc end}}
 
{{dsc end}}
  
 
====Bitwise shift operators====
 
====Bitwise shift operators====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc tfun|cpp/numeric/valarray/operator_arith3|title=operator<<<br>operator>>|applies binary operators to each element of two valarrays, or a valarray and a value}}
+
{{dsc tfun|cpp/numeric/valarray/operator arith3|title=operator<<<br>operator>>|applies binary operators to each element of two valarrays, or a valarray and a value}}
{{dsc mem fun|cpp/utility/bitset/operator_ltltgtgt|title=operator<<<br>operator>>|performs binary shift left and shift right}}
+
{{dsc mem fun|cpp/utility/bitset/operator ltltgtgt|title=operator<<<br>operator>>|performs binary shift left and shift right}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 484: Line 609:
  
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/io/basic_istream/dsc operator_gtgt}}
+
{{dsc inc|cpp/io/basic_istream/dsc operator gtgt}}
{{dsc inc|cpp/io/basic_istream/dsc operator_gtgt2}}
+
{{dsc inc|cpp/io/basic_istream/dsc operator gtgt2}}
{{dsc inc|cpp/io/basic_ostream/dsc operator_ltlt}}
+
{{dsc inc|cpp/io/basic_ostream/dsc operator ltlt}}
{{dsc inc|cpp/io/basic_ostream/dsc operator_ltlt2}}
+
{{dsc inc|cpp/io/basic_ostream/dsc operator ltlt2}}
{{dsc inc|cpp/numeric/complex/dsc operator_ltltgtgt}}
+
{{dsc inc|cpp/numeric/complex/dsc operator ltltgtgt}}
{{dsc inc|cpp/utility/bitset/dsc operator_ltltgtgt2}}
+
{{dsc inc|cpp/utility/bitset/dsc operator ltltgtgt2}}
{{dsc inc|cpp/string/basic_string/dsc operator_ltltgtgt}}
+
{{dsc inc|cpp/string/basic_string/dsc operator ltltgtgt}}
{{dsc inc|cpp/numeric/random/engine/dsc operator_ltltgtgt|linear_congruential_engine}}
+
{{dsc inc|cpp/numeric/random/engine/dsc operator ltltgtgt|linear_congruential_engine}}
{{dsc inc|cpp/numeric/random/distribution/dsc operator_ltltgtgt|uniform_int_distribution}}
+
{{dsc inc|cpp/numeric/random/distribution/dsc operator ltltgtgt|uniform_int_distribution}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 502: Line 627:
 
{{dr list item|wg=cwg|dr=1504|std=C++98|before=a pointer to a base class subobject of an array<br>element could be used in pointer arithmetic|after=the behavior is<br>undefined in this case}}
 
{{dr list item|wg=cwg|dr=1504|std=C++98|before=a pointer to a base class subobject of an array<br>element could be used in pointer arithmetic|after=the behavior is<br>undefined in this case}}
 
{{dr list item|wg=cwg|dr=1515|std=C++98|before=only unsigned integers which declared {{c/core|unsigned}} <br>should obey the laws of arithmetic {{math|modulo 2{{su|p=n}}}}|after=applies to all unsigned integers}}
 
{{dr list item|wg=cwg|dr=1515|std=C++98|before=only unsigned integers which declared {{c/core|unsigned}} <br>should obey the laws of arithmetic {{math|modulo 2{{su|p=n}}}}|after=applies to all unsigned integers}}
 +
{{dr list item|wg=cwg|dr=1642|std=C++98|before=arithmetic operators allow their operands to be lvalues|after=some operands must be rvalues}}
 
{{dr list item|wg=cwg|dr=1865|std=C++98|before=the resolution of {{cwg|1504}} made the behaviors<br>of pointer arithmetic involving pointers to array element<br>undefined if the pointed-to type and the array element<br>type have different cv-qualifications in non-top levels|after=made well-defined}}
 
{{dr list item|wg=cwg|dr=1865|std=C++98|before=the resolution of {{cwg|1504}} made the behaviors<br>of pointer arithmetic involving pointers to array element<br>undefined if the pointed-to type and the array element<br>type have different cv-qualifications in non-top levels|after=made well-defined}}
 
{{dr list item|wg=cwg|dr=1971|std=C++98|before=it was unclear whether the rule resolving the<br>ambiguity of {{c|~}} applies to cases such as {{c|~X(0)}}|after=the rule applies to such cases}}
 
{{dr list item|wg=cwg|dr=1971|std=C++98|before=it was unclear whether the rule resolving the<br>ambiguity of {{c|~}} applies to cases such as {{c|~X(0)}}|after=the rule applies to such cases}}
 
{{dr list item|wg=cwg|dr=2419|std=C++98|before=a pointer to non-array object was only treated as a<br>pointer to the first element of an array with size 1<br>in pointer arithmetic if the pointer is obtained by {{c|&}}|after=applies to all pointers<br>to non-array objects}}
 
{{dr list item|wg=cwg|dr=2419|std=C++98|before=a pointer to non-array object was only treated as a<br>pointer to the first element of an array with size 1<br>in pointer arithmetic if the pointer is obtained by {{c|&}}|after=applies to all pointers<br>to non-array objects}}
 
{{dr list item|wg=cwg|dr=2626|std=C++98|before=the result of built-in {{tt|operator~}} was simply<br>'one's complement' without proper definition|after=the result is phrased in terms<br>of the base-2 representation}}
 
{{dr list item|wg=cwg|dr=2626|std=C++98|before=the result of built-in {{tt|operator~}} was simply<br>'one's complement' without proper definition|after=the result is phrased in terms<br>of the base-2 representation}}
 +
{{dr list item|wg=cwg|dr=2724|std=C++20|before=the rounding direction of arithmetic right shift was unclear|after=made clear}}
 +
{{dr list item|wg=cwg|dr=2853|std=C++98|before=a pointer past the end of an object could<br>not be added or subtracted with an integer|after=it can}}
 
{{dr list end}}
 
{{dr list end}}
  

Latest revision as of 23:30, 24 June 2024

 
 
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
 
 

Returns the result of specific arithmetic operation.

Operator name Syntax Prototype examples (for class T)
Inside class definition Outside class definition
Unary plus +a T T::operator+() const; T operator+(const T& a);
Unary minus -a T T::operator-() const; T operator-(const T& a);
Addition a + b T T::operator+(const T2& b) const; T operator+(const T& a, const T2& b);
Subtraction a - b T T::operator-(const T2& b) const; T operator-(const T& a, const T2& b);
Multiplication a * b T T::operator*(const T2& b) const; T operator*(const T& a, const T2& b);
Division a / b T T::operator/(const T2& b) const; T operator/(const T& a, const T2& b);
Remainder a % b T T::operator%(const T2& b) const; T operator%(const T& a, const T2& b);
Bitwise NOT ~a T T::operator~() const; T operator~(const T& a);
Bitwise AND a & b T T::operator&(const T2& b) const; T operator&(const T& a, const T2& b);
Bitwise OR a | b T T::operator|(const T2& b) const; T operator|(const T& a, const T2& b);
Bitwise XOR a ^ b T T::operator^(const T2& b) const; T operator^(const T& a, const T2& b);
Bitwise left shift a << b T T::operator<<(const T2& b) const; T operator<<(const T& a, const T2& b);
Bitwise right shift a >> b T T::operator>>(const T2& b) const; T operator>>(const T& a, const T2& b);
Notes
  • All operators in this table are overloadable.
  • All built-in operators return values, and most user-defined overloads also return values 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). In particular, stream insertion and stream extraction overloads of operator<< and operator>> return T&.
  • T2 can be any type including T.

Contents

[edit] General explanation

All built-in arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.

[edit] Conversions

If the operand passed to a built-in arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion. If an operand has array or function type, array-to-pointer and function-to-pointer conversions are applied.

For the binary operators (except shifts), if the promoted operands have different types, usual arithmetic conversions are applied.

[edit] Overflows

Unsigned integer arithmetic is always performed modulo 2n where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives 0, and subtracting one from 0 gives UINT_MAX.

When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include:

  • it wraps around according to the rules of the representation (typically two's complement),
  • it traps — on some platforms or due to compiler options (e.g. -ftrapv in GCC and Clang),
  • it saturates to minimal or maximal value (on many DSPs),
  • it is completely optimized out by the compiler.

[edit] Floating-point environment

If #pragma STDC FENV_ACCESS is supported and set to ON, all floating-point arithmetic operators obey the current floating-point rounding direction and report floating-point arithmetic errors as specified in math_errhandling unless part of a static initializer (in which case floating-point exceptions are not raised and the rounding mode is to nearest).

[edit] Floating-point contraction

Unless #pragma STDC FP_CONTRACT is supported and set to OFF, all floating-point arithmetic may be performed as if the intermediate results have infinite range and precision, that is, optimizations that omit rounding errors and floating-point exceptions are allowed. For example, C++ allows the implementation of (x * y) + z with a single fused multiply-add CPU instruction or optimization of a = x * x * x * x; as tmp = x * x; a = tmp * tmp.

Unrelated to contracting, intermediate results of floating-point arithmetic may have range and precision that is different from the one indicated by its type, see FLT_EVAL_METHOD.

Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations.

[edit] Unary arithmetic operators

The unary arithmetic operator expressions have the form

+ expression (1)
- expression (2)
1) Unary plus (promotion).
2) Unary minus (negation).

Unary + and - operators have higher precedence than all binary arithmetic operators, so expression cannot contain top-level binary arithmetic operators. These operators associate from right to left:

+a - b; // equivalent to (+a) - b, NOT +(a - b)
-c + d; // equivalent to (-c) + d, NOT -(c + d)
 
+-e; // equivalent to +(-e), the unary + is a no-op if “e” is a built-in type
     // because any possible promotion is performed during negation already

[edit] Built-in unary arithmetic operators

1) For the built-in unary plus operator, expression must be a prvalue of arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on expression if it has integral or unscoped enumeration type. The type of the result is the (possibly promoted) type of expression.
The result of the built-in promotion is the value of expression. The built-in unary operation is no-op if the operand is a prvalue of a promoted integral type or a pointer type. Otherwise, the type or value category of the operand is changed by integral promotion or lvalue-to-rvalue, array-to-pointer, function-to-pointer, or user-defined conversion. For example, char is converted to int , and non-generic captureless lambda expression is converted to function pointer(since C++11) in unary plus expressions.
2) For the built-in unary minus operator, expression must be a prvalue of arithmetic or unscoped enumeration type. Integral promotion is performed on expression. The type of the result is the type of the promoted type of expression.
The result of the built-in negation is the negative of the promoted expression. For unsigned a, the value of -a is 2N-a, where N is the number of bits after promotion.
  • In other words, the result is the two’s complement of the operand (where operand and result are considered as unsigned).

[edit] Overloads

In overload resolution against user-defined operators, for every cv-unqualified promoted arithmetic type A and for every type T, the following function signatures participate in overload resolution:

A operator+(A)
T* operator+(T*)
A operator-(A)
#include <iostream>
 
int main()
{
    char c = 0x6a;
    int n1 = 1;
    unsigned char n2 = 1;
    unsigned int n3 = 1;
    std::cout << "char: " << c << " int: " << +c << "\n"
                 "-1, where 1 is signed: " << -n1 << "\n"
                 "-1, where 1 is unsigned char: " << -n2 << "\n"
                 "-1, where 1 is unsigned int: " << -n3 << '\n';
    char a[3];
    std::cout << "size of array: " << sizeof a << "\n"
                 "size of pointer: " << sizeof +a << '\n';
}

Possible output:

char: j int: 106
-1, where 1 is signed: -1
-1, where 1 is unsigned char: -1
-1, where 1 is unsigned int: 4294967295
size of array: 3
size of pointer: 8

[edit] Additive operators

The additive operator expressions have the form

lhs + rhs (1)
lhs - rhs (2)
1) Binary plus (addition).
2) Binary minus (subtraction).

Binary + and - operators have higher precedence than all other binary arithmetic operators except *, / and %. These operators associate from left to right:

a + b * c;  // equivalent to a + (b * c),  NOT (a + b) * c
d / e - f;  // equivalent to (d / e) - f,  NOT d / (e - f)
g + h >> i; // equivalent to (g + h) >> i, NOT g + (h >> i)
 
j - k + l - m; // equivalent to ((j - k) + l) - m

[edit] Built-in additive operators

For built-in binary plus and binary minus operators, both of lhs and rhs must be prvalues, and one of the following conditions must be satisfied:

  • Both operands have arithmetic or unscoped enumeration type. In this case, usual arithmetic conversions are performed on both operands.
  • Exactly one operand has integral or unscoped enumeration type. In this case, integral promotion is applied to that operand.

In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted or promoted operand(s).

1) For built-in addition, one of the following conditions must be satisfied:
  • Both operands have arithmetic type. In this case, the result is the sum of the operands.
  • One operand is a pointer to a completely-defined object type, and the other operand has integral type. In this case, the integral value is added to the pointer (see pointer arithmetic).
2) For built-in subtraction, one of the following conditions must be satisfied:
  • Both operands have arithmetic type. In this case, the result is the difference resulting from the subtraction of rhs from lhs.
  • lhs is a pointer to a completely-defined object type, and rhs has integral type. In this case, the integral value is subtracted from the pointer (see pointer arithmetic).
  • Both operands are pointers to cv-qualified or cv-unqualified versions of the same completely-defined object type. In this case rhs is subtracted from lhs (see pointer arithmetic).

If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see std::numeric_limits::is_iec559):

  • If one operand is NaN, the result is NaN.
  • Infinity minus infinity is NaN, and FE_INVALID is raised.
  • Infinity plus the negative infinity is NaN, and FE_INVALID is raised.

[edit] Pointer arithmetic

When an expression J that has integral type is added to or subtracted from an expression P of pointer type, the result has the type of P.

  • If P evaluates to a null pointer value and J evaluates to 0, the result is a null pointer value.
  • Otherwise, if P points to the ith element of an array object x with n elements, given the value of J as j, P is added or subtracted as follows:
  • The expressions P + J and J + P
  • point to the i+jth element of x if i + j is in [0n), and
  • are pointers past the end of the last element of x if i + j is n.
  • The expression P - J
  • points to the i-jth element of x if i - j is in [0n), and
  • is a pointer past the end of the last element of x if i - j is n.
  • Other j values result in undefined behavior.
  • Otherwise, if P points to a complete object, a base class subobject or a member subobject y, given the value of J as j, P is added or subtracted as follows:
  • The expressions P + J and J + P
  • point to y if j is 0, and
  • are pointers past the end of y if j is 1.
  • The expression P - J
  • points to y if j is 0, and
  • is a pointer past the end of y if j is -1.
  • Other j values result in undefined behavior.
  • Otherwise, if P is a pointer past the end of an object z, given the value of J as j:
  • If z is an array object with n elements, P is added or subtracted as follows:
  • The expressions P + J and J + P
  • point to the n+jth element of z if n + j is in [0n), and
  • are pointers past the end of the last element of z if j is 0.
  • The expression P - J
  • points to the n-jth element of z if n - j is in [0n), and
  • is a pointer past the end of the last element of z if j is 0.
  • Other j values result in undefined behavior.
  • Otherwise, P is added or subtracted as follows:
  • The expressions P + J and J + P
  • point to z if j is -1, and
  • are pointers past the end of z if j is 0.
  • The expression P - J
  • points to z if j is 1, and
  • is a pointer past the end of z if j is 0.
  • Other j values result in undefined behavior.
  • Otherwise, the behavior is undefined.

When two pointer expressions P and Q are subtracted, the type of the result is std::ptrdiff_t.

  • If P and Q both evaluate to null pointer values, the result is 0.
  • Otherwise, if P and Q point to, respectively, the ith and jth array elements of the same array object x, the expression P - Q has the value i − j.
  • If i − j is not representable by std::ptrdiff_t, the behavior is undefined.
  • Otherwise, if P and Q point to the same complete object, base class subobject or member subobject, the result is 0.
  • Otherwise, the behavior is undefined.

These pointer arithmetic operators allow pointers to satisfy the LegacyRandomAccessIterator requirements.

For addition and subtraction, if P or Q have type “pointer to (possibly cv-qualified) T”, where T and the array element type are not similar, the behavior is undefined:

int arr[5] = {1, 2, 3, 4, 5};
unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1);
unsigned int k = *p; // OK, the value of “k” is 2
unsigned int *q = p + 1; // undefined behavior: “p” points to int, not unsigned int

[edit] Overloads

In overload resolution against user-defined operators, for every pair of promoted arithmetic types L and R and for every object type T, the following function signatures participate in overload resolution:

LR operator+(L, R)
LR operator-(L, R)
T* operator+(T*, std::ptrdiff_t)
T* operator+(std::ptrdiff_t, T*)
T* operator-(T*, std::ptrdiff_t)
std::ptrdiff_t operator-(T*, T*)

where LR is the result of usual arithmetic conversions on L and R.

#include <iostream>
 
int main()
{
    char c = 2;
    unsigned int un = 2;
    int n = -10;
    std::cout << " 2 + (-10), where 2 is a char    = " << c + n << "\n"
                 " 2 + (-10), where 2 is unsigned  = " << un + n << "\n"
                 " -10 - 2.12  = " << n - 2.12 << '\n';
 
    char a[4] = {'a', 'b', 'c', 'd'};
    char* p = &a[1];
    std::cout << "Pointer addition examples: " << *p << *(p + 2)
              << *(2 + p) << *(p - 1) << '\n';
    char* p2 = &a[4];
    std::cout << "Pointer difference: " << p2 - p << '\n';
}

Output:

 2 + (-10), where 2 is a char    = -8
 2 + (-10), where 2 is unsigned  = 4294967288
 -10 - 2.12  = -12.12
Pointer addition examples: bdda
Pointer difference: 3

[edit] Multiplicative operators

The multiplicative operator expressions have the form

lhs * rhs (1)
lhs / rhs (2)
lhs % rhs (3)
1) Multiplication.
2) Division.
3) Remainder.

Multiplicative operators have higher precedence than all other binary arithmetic operators. These operators associate from left to right:

a + b * c;  // equivalent to a + (b * c),  NOT (a + b) * c
d / e - f;  // equivalent to (d / e) - f,  NOT d / (e - f)
g % h >> i; // equivalent to (g % h) >> i, NOT g % (h >> i)
 
j * k / l % m; // equivalent to ((j * k) / l) % m

[edit] Built-in multiplicative operators

For built-in multiplication and division operators, both operands must have arithmetic or unscoped enumeration type. For the built-in remainder operator, both operands must have integral or unscoped enumeration type. Usual arithmetic conversions are performed on both operands.

In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted operand(s).

1) The result of built-in multiplication is the product of the operands.
If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see std::numeric_limits::is_iec559):
  • Multiplication of a NaN by any number gives NaN.
  • Multiplication of infinity by zero gives NaN and FE_INVALID is raised.
2) The result of built-in division is lhs divided by rhs. If rhs is zero, the behavior is undefined.
If both operands have an integral type, the result is the algebraic quotient (performs integer division): the quotient is truncated towards zero (fractional part is discarded).
If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see std::numeric_limits::is_iec559):
  • If one operand is NaN, the result is NaN.
  • Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and FE_DIVBYZERO is raised.
  • Dividing 0.0 by 0.0 gives NaN and FE_INVALID is raised.
3) The result of built-in remainder is the remainder of the integer division of lhs by rhs. If rhs is zero, the behavior is undefined.
If a / b is representable in the result type, (a / b) * b + a % b == a.
If a / b is not representable in the result type, the behavior of both a / b and a % b is undefined (that means INT_MIN % -1 is undefined on two's complement systems).

Note: Until CWG issue 614 was resolved (N2757), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function std::div provided well-defined behavior in that case.

Note: for floating-point remainder, see std::remainder and std::fmod.

[edit] Overloads

In overload resolution against user-defined operators, for every pair of promoted arithmetic types LA and RA and for every pair of promoted integral types LI and RI the following function signatures participate in overload resolution:

LRA operator*(LA, RA)
LRA operator/(LA, RA)
LRI operator%(LI, RI)

where LRx is the result of usual arithmetic conversions on Lx and Rx.

#include <iostream>
 
int main()
{
    char c = 2;
    unsigned int un = 2;
    int  n = -10;
    std::cout << "2 * (-10), where 2 is a char    = " << c * n << "\n"
                 "2 * (-10), where 2 is unsigned  = " << un * n << "\n"
                 "-10 / 2.12  = " << n / 2.12 << "\n"
                 "-10 / 21  = " << n / 21 << "\n"
                 "-10 % 21  = " << n % 21 << '\n';
}

Output:

2 * (-10), where 2 is a char    = -20
2 * (-10), where 2 is unsigned  = 4294967276
-10 / 2.12  = -4.71698
-10 / 21  = 0
-10 % 21  = -10

[edit] Bitwise logic operators

The bitwise logic operator expressions have the form

~ rhs (1)
lhs & rhs (2)
lhs | rhs (3)
lhs ^ rhs (4)
1) Bitwise NOT.
2) Bitwise AND.
3) Bitwise OR.
4) Bitwise XOR.

The bitwise NOT operator has higher precedence than all binary arithmetic operators. It associates from right to left:

~a - b; // equivalent to (~a) - b, NOT ~(a - b)
~c * d; // equivalent to (~c) * d, NOT ~(c * d)
 
~-e; // equivalent to ~(-e)

There is an ambiguity in the grammar when ~ is followed by a type name or decltype specifier(since C++11): it can either be operator~ or start a destructor identifier). The ambiguity is resolved by treating ~ as operator~. ~ can start a destructor identifier only in places where forming an operator~ is syntactically invalid.

All other bitwise logic operators have lower precedence than all other binary arithmetic operators. Bitwise AND has higher precedence than bitwise XOR, which has higher precedence than bitwise OR. They associate from left to right:

a & b * c;  // equivalent to a & (b * c),  NOT (a & b) * c
d / e ^ f;  // equivalent to (d / e) ^ f,  NOT d / (e ^ f)
g << h | i; // equivalent to (g << h) | i, NOT g << (h | i)
 
j & k & l; // equivalent to (j & k) & l
m | n ^ o  // equivalent to m | (n ^ o)

[edit] Built-in bitwise logic operators

For the built-in bitwise NOT operator, rhs must be a prvalue of integral or unscoped enumeration type, and integral promotion is performed on rhs. For other built-in bitwise logic operators, both operands must have integral or unscoped enumeration type, and usual arithmetic conversions are performed on both operands.

In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted or promoted operand(s).

1) Given the operand as x and the result of the built-in bitwise NOT operation as r. For each coefficient x_i of the base-2 representation of x, the corresponding coefficient r_i of the base-2 representation of r is 1 if x_i is 0, and 0 otherwise.
  • In other words, the result is the one’s complement of the operand (where operand and result are considered as unsigned).
The type of the result r is the type of the operand x.
2-4) Given the operands as x and y respectively and the result of the built-in binary bitwise logic operations as r. For each pair of coefficients x_i and y_i of the base-2 representations of x and y respectively, the corresponding coefficient r_i of the base-2 representation of r is
2) 1 if both x_i and y_i are 1, and 0 otherwise.
3) 1 if at least one of x_i and y_i is 1, and 0 otherwise.
4) 1 if either (but not both) of x_i and y_i is 1, and 0 otherwise.
The type of the result r is the type of the operands x and y.

[edit] Overloads

In overload resolution against user-defined operators, for every pair of promoted integral types L and R the following function signatures participate in overload resolution:

R operator~(R)
LR operator&(L, R)
LR operator^(L, R)
LR operator|(L, R)

where LR is the result of usual arithmetic conversions on L and R.

#include <bitset>
#include <cstdint>
#include <iomanip>
#include <iostream>
 
int main()
{
    std::uint16_t mask = 0x00f0;
    std::uint32_t x0 = 0x12345678;
    std::uint32_t x1 = x0 | mask;
    std::uint32_t x2 = x0 & ~mask;
    std::uint32_t x3 = x0 & mask;
    std::uint32_t x4 = x0 ^ mask;
    std::uint32_t x5 = ~x0;
    using bin16 = std::bitset<16>;
    using bin32 = std::bitset<32>;
    std::cout << std::hex << std::showbase
              << "Mask: " << mask << std::setw(49) << bin16(mask) << "\n"
                 "Value: " << x0 << std::setw(42) << bin32(x0) << "\n"
                 "Setting bits: " << x1 << std::setw(35) << bin32(x1) << "\n"
                 "Clearing bits: " << x2 << std::setw(34) << bin32(x2) << "\n"
                 "Selecting bits: " << x3 << std::setw(39) << bin32(x3) << "\n"
                 "XOR-ing bits: " << x4 << std::setw(35) << bin32(x4) << "\n"
                 "Inverting bits: " << x5 << std::setw(33) << bin32(x5) << '\n';
}

Output:

Mask: 0xf0                                 0000000011110000
Value: 0x12345678          00010010001101000101011001111000
Setting bits: 0x123456f8   00010010001101000101011011111000
Clearing bits: 0x12345608  00010010001101000101011000001000
Selecting bits: 0x70       00000000000000000000000001110000
XOR-ing bits: 0x12345688   00010010001101000101011010001000
Inverting bits: 0xedcba987 11101101110010111010100110000111

[edit] Bitwise shift operators

The bitwise shift operator expressions have the form

lhs << rhs (1)
lhs >> rhs (2)
1) Bitwise left-shift.
2) Bitwise right-shift.

Bitwise shift operators have higher precedence than bitwise logic operators, but have lower precedence than additive and multiplicative operators. These operators associate from left to right:

a >> b * c;  // equivalent to a >> (b * c),  NOT (a >> b) * c
d << e & f;  // equivalent to (d << e) & f,  NOT d << (e & f)
 
g << h >> i; // equivalent to (g << h) >> i, NOT g << (h >> i)

[edit] Built-in bitwise shift operators

For the built-in bitwise shift operators, both operands must be prvalues of integral or unscoped enumeration type. Integral promotions are performed on both operands.

In the remaining description in this section, "operand(s)", a, b, lhs and rhs refer to the converted or promoted operand(s).

If the value of rhs is negative or is not less than the number of bits in lhs, the behavior is undefined.

For unsigned a, the value of a << b is the value of a * 2b, reduced modulo 2N where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).

For signed and non-negative a, if a * 2b is representable in the unsigned version of the return type, then that value, converted to signed, is the value of a << b (this makes it legal to create INT_MIN as 1 << 31); otherwise the behavior is undefined.

For negative a, the behavior of a << b is undefined.

For unsigned a and for signed and non-negative a, the value of a >> b is the integer part of a/2b.

For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative).

(until C++20)

The value of a << b is the unique value congruent to a * 2b modulo 2N where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).

The value of a >> b is a/2b, rounded towards negative infinity (in other words, right shift on signed a is arithmetic right shift).

(since C++20)

The type of the result is that of lhs.

[edit] Overloads

In overload resolution against user-defined operators, for every pair of promoted integral types L and R, the following function signatures participate in overload resolution:

L operator<<(L, R)
L operator>>(L, R)
#include <iostream>
 
enum { ONE = 1, TWO = 2 };
 
int main()
{
    std::cout << std::hex << std::showbase;
    char c = 0x10;
    unsigned long long ull = 0x123;
    std::cout << "0x123 << 1 = " << (ull << 1) << "\n"
                 "0x123 << 63 = " << (ull << 63) << "\n" // overflow in unsigned
                 "0x10 << 10 = " << (c << 10) << '\n';   // char is promoted to int
    long long ll = -1000;
    std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n';
}

Output:

0x123 << 1 = 0x246
0x123 << 63 = 0x8000000000000000
0x10 << 10 = 0x4000
-1000 >> 1 = -500

[edit] Standard library

Arithmetic operators are overloaded for many standard library types.

[edit] Unary arithmetic operators

implements unary + and unary -
(public member function of std::chrono::duration<Rep,Period>) [edit]
applies unary operators to complex numbers
(function template) [edit]
applies a unary arithmetic operator to each element of the valarray
(public member function of std::valarray<T>) [edit]

[edit] Additive operators

performs add and subtract operations involving a time point
(function template) [edit]
implements arithmetic operations with durations as arguments
(function template) [edit]
adds or subtracts a year_month_day and some number of years or months
(function) [edit]
concatenates two strings, a string and a char, or a string and string_view
(function template) [edit]
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>)
performs complex number arithmetic on two complex values or a complex and a scalar
(function template) [edit]
applies binary operators to each element of two valarrays, or a valarray and a value
(function template) [edit]

[edit] Multiplicative operators

implements arithmetic operations with durations as arguments
(function template) [edit]
performs complex number arithmetic on two complex values or a complex and a scalar
(function template) [edit]
applies binary operators to each element of two valarrays, or a valarray and a value
(function template) [edit]

[edit] Bitwise logic operators

performs binary AND, OR, XOR and NOT
(public member function of std::bitset<N>) [edit]
performs binary logic operations on bitsets
(function template) [edit]
applies a unary arithmetic operator to each element of the valarray
(public member function of std::valarray<T>)
applies binary operators to each element of two valarrays, or a valarray and a value
(function template)

[edit] Bitwise shift operators

applies binary operators to each element of two valarrays, or a valarray and a value
(function template)
performs binary shift left and shift right
(public member function of std::bitset<N>)

[edit] Stream insertion/extraction operators

Throughout the standard library, bitwise shift operators are commonly overloaded with I/O stream (std::ios_base& or one of the classes derived from it) as both the left operand and return type. Such operators are known as stream insertion and stream extraction operators:

extracts formatted data
(public member function of std::basic_istream<CharT,Traits>) [edit]
extracts characters and character arrays
(function template) [edit]
inserts formatted data
(public member function of std::basic_ostream<CharT,Traits>) [edit]
inserts character data or insert into rvalue stream
(function template) [edit]
serializes and deserializes a complex number
(function template) [edit]
performs stream input and output of bitsets
(function template) [edit]
performs stream input and output on strings
(function template) [edit]
performs stream input and output on pseudo-random number engine
(function template) [edit]
performs stream input and output on pseudo-random number distribution
(function template) [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 614 C++98 the algebraic quotient of integer division was
rounded in implementation-defined direction
the algebraic quotient of integer
division is truncated towards zero
(fractional part is discarded)
CWG 1450 C++98 the result of a / b was unspecified if
it is not representable in the result type
the behavior of both a / b and
a % b is undefined in this case
CWG 1457 C++98 the behavior of shifting the leftmost 1 bit of a
positive signed value into the sign bit was undefined
made well-defined
CWG 1504 C++98 a pointer to a base class subobject of an array
element could be used in pointer arithmetic
the behavior is
undefined in this case
CWG 1515 C++98 only unsigned integers which declared unsigned
should obey the laws of arithmetic modulo 2n
applies to all unsigned integers
CWG 1642 C++98 arithmetic operators allow their operands to be lvalues some operands must be rvalues
CWG 1865 C++98 the resolution of CWG issue 1504 made the behaviors
of pointer arithmetic involving pointers to array element
undefined if the pointed-to type and the array element
type have different cv-qualifications in non-top levels
made well-defined
CWG 1971 C++98 it was unclear whether the rule resolving the
ambiguity of ~ applies to cases such as ~X(0)
the rule applies to such cases
CWG 2419 C++98 a pointer to non-array object was only treated as a
pointer to the first element of an array with size 1
in pointer arithmetic if the pointer is obtained by &
applies to all pointers
to non-array objects
CWG 2626 C++98 the result of built-in operator~ was simply
'one's complement' without proper definition
the result is phrased in terms
of the base-2 representation
CWG 2724 C++20 the rounding direction of arithmetic right shift was unclear made clear
CWG 2853 C++98 a pointer past the end of an object could
not be added or subtracted with an integer
it can

[edit] See also

Operator precedence

Operator overloading

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)

C documentation for Arithmetic operators