cpp/language/operator arithmetic

Returns the result of specific arithmetic operation.

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

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. If an operand has array or function type, and  conversions are applied.

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

Overflows
Unsigned integer arithmetic is always performed $modulo 2n$ where n is the number of bits in that particular integer. E.g. for, adding one to uint_max gives , and subtracting one from 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 2's complement),
 * it traps — on some platforms or due to compiler options (e.g. in GCC and Clang),
 * it saturates to minimal or maximal value (on many DSPs),
 * it is completely optimized out by the compiler.

Floating-point environment
If is supported and set to, all floating-point arithmetic operators obey the current floating-point rounding direction and report floating-point arithmetic errors as specified in  unless part of a  (in which case floating-point exceptions are not raised and the rounding mode is to nearest).

Floating-point contraction
Unless is supported and set to, 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 with a single fused multiply-add CPU instruction or optimization of  as.

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.

Unary arithmetic operators
The unary arithmetic operator expressions have the form

@1@ unary plus (promotion). @@ For the built-in operator, 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). @@ For the built-in operator, 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 to  or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.

The builtin unary minus operator calculates the negative of its promoted operand. For unsigned, the value of is $2b -a$, where  is the number of bits after promotion.

In, for every cv-unqualified promoted arithmetic type and for every type , the following function signatures participate in overload resolution:

Additive operators
The binary additive arithmetic operator expressions have the form

@1@ addition @@ For the built-in operator, and  must be one of the following:
 * both have arithmetic or unscoped enumeration type. In this case, the 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, and  must be one of the following:
 * both have arithmetic or unscoped enumeration type. In this case, the are performed on both operands and determine the type of the result.
 * is a pointer to completely-defined object type, 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 std.

With operands of arithmetic or enumeration type, the result of binary plus is the sum of the operands (after ), and the result of the binary minus operator is the result of subtracting the second operand from the first (after ), except that, if the type supports IEEE floating-point arithmetic (see std),
 * 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

If any of the operands is a pointer, the following rules apply: These pointer arithmetic operators allow pointers to satisfy the requirements.
 * A pointer to non-array object is treated as a pointer to the first element of an array with size 1.
 * If the pointer points to the th element of an array, then the expressions, , and  are pointers of the same type that point to the th, th, and th element of the same array, respectively. The result of pointer addition may also be a one-past-the-end pointer (that is, pointer  such that the expression  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 points to the th element of an array, and the pointer  points at the th element of the same array, the expression  has the value, if the value fits in std. 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, 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 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.

In, for every pair of promoted arithmetic types and  and for every object type , the following function signatures participate in overload resolution:

where is the result of  on  and.

Multiplicative operators
The binary multiplicative arithmetic operator expressions have the form

@1@ multiplication @@ For the built-in operator, and  must both have arithmetic or unscoped enumeration type. @2@ division @@ For the built-in operator, and  must both have arithmetic or unscoped enumeration type. @3@ remainder @@ For the built-in operator, and  must both have integral or unscoped enumeration type.

For all three operators, the are performed on both operands and determine the type of the result.

The binary operator * performs multiplication of its operands (after ), except that, for floating-point multiplication,
 * multiplication of a NaN by any number gives NaN
 * multiplication of infinity by zero gives NaN and fe_invalid is raised

The binary operator / divides the first operand by the second (after ).

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 floating-point division is taking place and the type supports IEEE floating-point arithmetic (see std), then:
 * 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

The binary operator % yields the remainder of the integer division of the first operand by the second (after ; note that the operand types must be integral types). If the quotient is representable in the result type,. If the second operand is zero, the behavior is undefined. If the quotient is not representable in the result type, the behavior of both  and  is undefined (that means  is undefined on 2's complement systems).

Note: Until was resolved, 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 provided well-defined behavior in that case.

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

In, for every pair of promoted arithmetic types and  and for every pair of promoted integral types  and  the following function signatures participate in overload resolution:

where is the result of  on  and.

Bitwise logic operators
The bitwise arithmetic operator expressions have the form

@1@ bitwise NOT @2@ bitwise AND @3@ bitwise OR @4@ bitwise XOR @@ For the built-in operators, and  must both have integral or unscoped enumeration type. 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 ). The result of operator| is the bitwise OR value of the operands (after ). The result of operator^ is the bitwise XOR value of the operands (after ).

There is an ambiguity in the grammar when is followed by a  : it can either be operator~ or start a  identifier). The ambiguity is resolved by treating  as operator~.  can start a destructor identifier only in places where forming an operator~ is syntactically invalid.

In, for every pair of promoted integral types and  the following function signatures participate in overload resolution:

where is the result of  on  and.

Bitwise shift operators
The bitwise shift operator expressions have the form

@1@ left shift of by  bits @2@ right shift of by  bits @@ For the built-in operators, and  must both have integral or unscoped enumeration type. Integral promotions are performed on both operands.

The return type is the type of the left operand after integral promotions.

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, for every pair of promoted integral types and , the following function signatures participate in overload resolution:

Standard library
Arithmetic operators are overloaded for many standard library types.

Stream insertion/extraction operators
Throughout the standard library, bitwise shift operators are commonly overloaded with I/O stream ( 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: