Returns the result of specific arithmetic operation.
Operator name  Syntax  Overloadable  Prototype examples (for class T ) 


Inside class definition  Outside class definition  
unary plus 
+a  Yes 
T T::operator+() const; 
T operator+(const T &a); 
unary minus 
a  Yes 
T T::operator() const; 
T operator(const T &a); 
addition 
a + b  Yes 
T T::operator+(const T2 &b) const; 
T operator+(const T &a, const T2 &b); 
subtraction 
a  b  Yes 
T T::operator(const T2 &b) const; 
T operator(const T &a, const T2 &b); 
multiplication 
a * b  Yes 
T T::operator*(const T2 &b) const; 
T operator*(const T &a, const T2 &b); 
division 
a / b  Yes 
T T::operator/(const T2 &b) const; 
T operator/(const T &a, const T2 &b); 
modulo 
a % b  Yes 
T T::operator%(const T2 &b) const; 
T operator%(const T &a, const T2 &b); 
bitwise NOT 
~a  Yes 
T T::operator~() const; 
T operator~(const T &a); 
bitwise AND 
a & b  Yes 
T T::operator&(const T2 &b) const; 
T operator&(const T &a, const T2 &b); 
bitwise OR 
a  b  Yes 
T T::operator(const T2 &b) const; 
T operator(const T &a, const T2 &b); 
bitwise XOR 
a ^ b  Yes 
T T::operator^(const T2 &b) const; 
T operator^(const T &a, const T2 &b); 
bitwise left shift 
a << b  Yes 
T T::operator<<(const T2 &b) const; 
T operator<<(const T &a, const T2 &b); 
bitwise right shift 
a >> b  Yes 
T T::operator>>(const T2 &b) const; 
T operator>>(const T &a, const T2 &b); 
Notes

All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.
If the operand passed to an arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvaluetorvalue conversion, if applicable), the operand undergoes integral promotion. If an operand has array or function type, arraytopointer and functiontopointer conversions are applied.
For the binary operators (except shifts), if the promoted operands have different types, additional set of implicit conversions is applied, known as usual arithmetic conversions with the goal to produce the common type (also accessible via the std::common_type
type trait). If, prior to any integral promotion, one operand is of enumeration type and the other operand is of a floatingpoint type or a different enumeration type, this behavior is deprecated. (since C++20).
long double
, the other operand is converted to long double
. double
, the other operand is converted to double
. float
, the other operand is converted to float
. bool
, char
, char8_t
, (since C++20) char16_t
, char32_t
, (since C++11) wchar_t
, and unscoped enumeration were promoted at this point) and integral conversions are applied to produce the common type, as follows: The conversion rank above increases in order bool
, signed char
, short
, int
, long
, long long
(since C++11). The rank of any unsigned type is equal to the rank of the corresponding signed type. The rank of char
is equal to the rank of signed char
and unsigned char
. The ranks of char8_t
, (since C++20)char16_t
, char32_t
, and (since C++11)wchar_t
are equal to the ranks of their corresponding underlying types.
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:
ftrapv
in GCC and Clang), If #pragma STDC FENV_ACCESS
is supported and set to ON
, all floatingpoint arithmetic operators obey the current floatingpoint rounding direction and report floatingpoint arithmetic errors as specified in math_errhandling
unless part of a static initializer (in which case floatingpoint exceptions are not raised and the rounding mode is to nearest).
Unless #pragma STDC FP_CONTRACT
is supported and set to OFF
, all floatingpoint arithmetic may be performed as if the intermediate results have infinite range and precision, that is, optimizations that omit rounding errors and floatingpoint exceptions are allowed. For example, C++ allows the implementation of (x * y) + z
with a single fused multiplyadd CPU instruction or optimization of a = x * x * x * x;
as tmp = x * x; a = tmp * tmp
.
Unrelated to contracting, intermediate results of floatingpoint 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 floatingpoint operations.
The unary arithmetic operator expressions have the form.
+ expression  (1)  
 expression  (2) 
The builtin unary plus operator returns the value of its operand. The only situation where it is not a noop is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, e.g, it converts char
to int
or if the operand is subject to lvaluetorvalue, arraytopointer, or functiontopointer conversion.
The builtin unary minus operator calculates the negative of its promoted operand. For unsigned a
, the value of a
is 2b
a, where b
is the number of bits after promotion.
In overload resolution against userdefined operators, for every cvunqualified 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
The binary additive arithmetic operator expressions have the form.
lhs + rhs  (1)  
lhs  rhs  (2) 
std::ptrdiff_t
.With operands of arithmetic or enumeration type, the result of binary plus is the sum of the operands (after usual arithmetic conversions), and the result of the binary minus operator is the result of subtracting the second operand from the first (after usual arithmetic conversions), except that, if the type supports IEEE floatingpoint arithmetic (see std::numeric_limits::is_iec559
),
FE_INVALID
is raised FE_INVALID
is raised If any of the operands is a pointer, the following rules apply:
P
points to the i
th element of an array, then the expressions P + n
, n + P
, and P  n
are pointers of the same type that point to the i+n
th, i+n
th, and in
th element of the same array, respectively. The result of pointer addition may also be a onepasttheend pointer (that is, pointer P
such that the expression 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. P
points to the i
th element of an array, and the pointer Q
points at the j
th element of the same array, the expression P  Q
has the value i  j
, if the value fits in 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 std::ptrdiff_t
, the behavior is undefined. 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 (std::ptrdiff_t)0
. These pointer arithmetic operators allow pointers to satisfy the LegacyRandomAccessIterator requirements.
In overload resolution against userdefined 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
The binary multiplicative arithmetic operator expressions have the form.
lhs * rhs  (1)  
lhs / rhs  (2)  
lhs % rhs  (3) 
For all three operators, the usual arithmetic conversions are performed on both operands and determine the type of the result.
The binary operator * performs multiplication of its operands (after usual arithmetic conversions), except that, for floatingpoint multiplication,
FE_INVALID
is raised The binary operator / divides the first operand by the second (after usual arithmetic conversions).
For integral operands, it yields the algebraic quotient. The quotient is truncated towards zero (fractional part is discarded).
If the second operand is zero, the behavior is undefined, except that if floatingpoint division is taking place and the type supports IEEE floatingpoint arithmetic (see std::numeric_limits::is_iec559
), then:
FE_DIVBYZERO
is raised FE_INVALID
is raised The binary operator % yields the remainder of the integer division of the first operand by the second (after usual arithmetic conversions; note that the operand types must be integral types). If the quotient a / b
is representable in the result type, (a / b) * b + a % b == a
. If the second operand is zero, the behavior is undefined. If the quotient 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 2'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 implementationdefined, as it depends on the rounding direction of integer division. The function std::div
provided welldefined behavior in that case.
Note: for floatingpoint remainder, see std::remainder
and std::fmod
.
In overload resolution against userdefined 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
The bitwise arithmetic operator expressions have the form.
~ rhs  (1)  
lhs & rhs  (2)  
lhs  rhs  (3)  
lhs ^ rhs  (4) 
The result of operator~ is the bitwise NOT (one's complement) value of the argument (after promotion). The result of operator& is the bitwise AND value of the operands (after usual arithmetic conversions). The result of operatoris the bitwise OR value of the operands (after usual arithmetic conversions). The result of operator^ is the bitwise XOR value of the operands (after usual arithmetic conversions).
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 syntatically invalid.
In overload resolution against userdefined 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 <iostream> #include <iomanip> #include <bitset> int main() { uint16_t mask = 0x00f0; uint32_t x0 = 0x12345678; uint32_t x1 = x0  mask; uint32_t x2 = x0 & ~mask; uint32_t x3 = x0 & mask; uint32_t x4 = x0 ^ mask; 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' << "XORing 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 XORing bits: 0x12345688 00010010001101000101011010001000 Inverting bits: 0xedcba987 11101101110010111010100110000111
The bitwise shift operator expressions have the form.
lhs << rhs  (1)  
lhs >> rhs  (2) 
The return type is the type of the left operand after integral promotions.
For unsigned For signed and nonnegative For negative For unsigned For negative  (until C++20) 
The value of The value of  (since C++20) 
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.
In overload resolution against userdefined 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
Arithmetic operators are overloaded for many standard library types.
implements unary + and unary  (public member function of std::chrono::duration<Rep,Period> ) 

applies unary operators to complex numbers (function template) 

applies a unary arithmetic operator to each element of the valarray (public member function of std::valarray<T> ) 
(C++11)  performs add and subtract operations involving a time point (function template) 
(C++11)  implements arithmetic operations with durations as arguments (function template) 
(C++20)  adds or subtracts a year_month_day and some number of years or months (function) 
concatenates two strings or a string and a char (function template) 

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 arithmetics on two complex values or a complex and a scalar (function template) 

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

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

performs binary logic operations on bitsets (function template) 

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

extracts characters and character arrays (function template) 

inserts formatted data (public member function of std::basic_ostream<CharT,Traits> ) 

inserts character data or insert into rvalue stream (function template) 

serializes and deserializes a complex number (function template) 

performs stream input and output of bitsets (function template) 

performs stream input and output on strings (function template) 

(C++11)  performs stream input and output on pseudorandom number engine (function template) 
(C++11)  performs stream input and output on pseudorandom number distribution (function template) 
The following behaviorchanging 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 implementationdefined 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 ifit is not representable in the result type  the behavior of both a / b anda % b is undefined in this case 
CWG 1457  C++98  the behavior of shifting the leftmost 1 bit of apositive signed value into the sign bit was undefined  made welldefined 
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 1865  C++98  the resolution of CWG issue 1504 made the behaviors of pointer arithmetic involving pointers to array element undefined if the pointedto type and the array element type have different cvqualifications in nontop levels  made welldefined 
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 nonarray 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 nonarray objects 
Common operators  

assignment  increment decrement  arithmetic  logical  comparison  member access  other 







Special operators  

C documentation for Arithmetic operators 
© cppreference.com
Licensed under the Creative Commons AttributionShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/operator_arithmetic