Arithmetic operators
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
返回特定的算术运算的结果.
原文:
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); |
|
目录 |
[编辑] 解释
所有的算术运算符计算的结果,具体的算术运算,并返回其结果。不被修改的参数.
原文:
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 lvalue-to-rvalue conversion, if applicable), the operand undergoes 积分的推广. If an operand has array or function type, array-to-pointer and function-to-pointer conversions are applied.
对于二进制运算符(除移位)推广的操作数,如果有不同的类型,另外一组的隐式转换的应用,被称为“通常的算术转换”的目标产生的“普通型”(也可通过std::common_type的类型trait)
原文:
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 either operand has scoped enumeration type, no conversion is performed: the other operand and the return type must have the same type
- 否则,如果操作数是long double,则另一个操作数转换为long double原文:Otherwise, if either operand is long double, the other operand is converted to long double
- 否则,如果操作数是double,则另一个操作数转换为double原文:Otherwise, if either operand is double, the other operand is converted to double
- 否则,如果操作数是float,则另一个操作数转换为float原文:Otherwise, if either operand is float, the other operand is converted to float
- 如果两个操作数进行签名或两者都是无符号的,较小的操作数“转换排名”转换为操作数的整数转换排名原文:If both operands are signed or both are unsigned, the operand with lesser conversion rank is converted to the operand with the greater integer conversion rank
-
- 否则,如果无符号操作数的转换排名是大于或等于的符号操作数的转换排名,符号操作数转换为无符号操作数的类型.原文:Otherwise, if the unsigned operand's conversion rank is greater or equal to the conversion rank of the signed operand, the signed operand is converted to the unsigned operand's type.
-
- 否则,如果签署的操作数的类型可以代表所有的无符号操作数的值,无符号的操作数转换为签名者操作数的类型原文:Otherwise, if the signed operand's type can represent all values of the unsigned operand, the unsigned operand is converted to the signer operand's type
-
- 否则,两个操作数都转换为对应的无符号的符号操作数的类型.原文:Otherwise, both operands are converted to the unsigned counterpart of the signed operand's type.
-
上述增幅,以“转换排名”bool,signed char,short,int,long,long long。任何无符号类型的排名是等于相应的有符号类型的排名。 char的秩等于signed char的排名和unsigned char。行列char16_t,char32_t,并wchar_t等于其基本类型的行列.....
原文:
The conversion rank above increases in order bool, signed char, short, int, long, long long. 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 char16_t, char32_t, and wchar_t are equal to the ranks of their underlying types.
[编辑] 溢出
无符号的整数运算,总是执行modulo 2n
其中n是该特定的整数中的比特的数量。例如unsigned int,加一UINT_MAX使0,减10,UINT_MAX.
其中n是该特定的整数中的比特的数量。例如unsigned int,加一UINT_MAX使0,减10,UINT_MAX.
原文:
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.
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.
当签署整数算术操作溢出的结果并没有适合的结果类型,行为是不确定的:它可以包裹周围的表示(通常是2的补码)规则,它可以捕获一些平台或由于编译器选项(例如
-ftrapv
GCC和Clang的),也可能是完全optimized out by the compiler.原文:
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined: it may wrap around according to the rules of the representation (typically 2's complement), it may trap on some platforms or due to compiler options (e.g.
-ftrapv
in GCC and Clang), or may be completely optimized out by the compiler.[编辑] 一元算术运算符
对于每一个推广的算术类型
A
和每一种类型T
,下面的函数签名参与重载解析原文:
For every 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) |
||
内置一元加运算符返回其操作数的值。唯一的情况下,它是不执行任何操作,当操作数的整型或无作用域的枚举类型,这是改变积分的推广,例如,它可以将charint如果操作数是左值,右值,数组,指针,函数指针转换.
原文:
The builtin 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 char to int or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.
内建元负运算符计算其操作数负。对于无符号的
-a,
a
的价值-a
2b-a,
b
是数位晋升后.原文:
The builtin unary minus operator calculates the negative of its operand. For unsigned
-a, where
a
, the value of -a
is 2b-a, where
b
is the number of bits after promotion.#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'; }
输出:
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
[编辑] 加法运算符
对于每一个对促进算术类型
L
和R
,并为每个对象类型T
,下面的函数签名参与重载解析原文:
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*) |
||
LR
是通常的算术转换L
和R
的结果原文:
where
LR
is the result of usual arithmetic conversions on L
and R
随着操作数的算术运算或枚举类型,则结果的二进制加是操作数的总和(后通常的算术converesions),和二进制减去操作的结果是(通常的算术转换后的结果中减去第二个操作数从第一).
原文:
With operands of arithmetic or enumeration type, the result of binary plus is the sum of the operands (after usual arithmetic converesions), and the result of the binary minus operator is the result of subtracting the second operand from the first (after usual arithmetic conversions).
如果任何一个操作数是一个指针,适用以下规则:
原文:
If any of the operands is a pointer, the following rules apply:
- 非数组对象的指针被视为一个指针,指向一个数组的第一个元素的大小为1.原文:A pointer to non-array object is treated as a pointer to the first element of an array with size 1.
- 如果数组元素
P
th指针i
点,然后表达式P+n
,n+P
,并P-n
的同一类型的指向的i+n
th,i+n
th,和i-n
th的元素相同的数组,分别是指针。指针相加的结果,也可以是过去的结束指针(即,指向P
使得表达P-1
点阵列的最后一个元素)。在任何其他情况下(即,试图产生一个指针,不指向相同的数组的一个元素或一个过去的结束)调用未定义的行为.原文:If the pointerP
points to thei
th element of an array, then the expressionsP+n
,n+P
, andP-n
are pointers of the same type that point to thei+n
th,i+n
th, andi-n
th element of the same array, respectively. The result of pointer addition may also be a one-past-the-end pointer (that is, pointerP
such that the expressionP-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
点的i
th数组的元素,和在Q
th相同的数组元素的指针j
点,表达P-Q
具有的值i-j,如果该值适合在std::ptrdiff_t。这两个操作数必须指向同一个数组中的元素(或一个过去的结束),否则该行为是未定义的。如果结果不符合std::ptrdiff_t,该行为是未定义.原文:If the pointerP
points to thei
th element of an array, and the pointerQ
points at thej
th element of the same array, the expressionP-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加上或减去从一个指针,其结果是指针,不变。如果两个指针指向相同的对象或既过去的端部相同的数组,或两者都为空指针,然后减法的结果是等于(std::ptrdiff_t)0.原文:If the value 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.
这些指针的算术运算符允许指针,以满足
RandomAccessIterator
概念.原文:
These pointer arithmetic operators allow pointers to satisfy the
RandomAccessIterator
concept.#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'; }
输出:
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
[编辑] 乘法运算符
对于每一个对促进算术类型
LA
RA
和每对促进整数类型LI
和RI
下面的函数签名参与重载解析:原文:
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) |
||
LRx
是通常的算术转换Lx
和Rx
的结果原文:
where
LRx
is the result of usual arithmetic conversions on Lx
and Rx
二元运算符*进行乘法运算的操作数(通常的算术转换后).
原文:
The binary operator * performs multiplication of its operands (after usual arithmetic conversions).
二元运算符/分的第一个操作数第二个(通常的算术转换后)。如果第二个操作数为零,则该行为是未定义的。对于整型操作数,它产生的代数商
原文:
The binary operator / divides the first operand by the second (after usual arithmetic conversions).If the second operand is zero, the behavior is undefined. For integral operands, it yields the algebraic quotient
- 圆形的实现定义的方向(至 C++11)原文:rounded in implementation-defined direction (至 C++11)
- 任何小数部分被丢弃(接近零截断)(C++11 起)原文:with any fractional part discarded (truncated towards zero) (C++11 起)
二元运算符%的收益,其余的第二个(通常的算术转换)后的第一个操作数的划分。如果商
a/b
的representible的的结果类型中,(a/b)*b + a%b == a。如果第二个操作数为零,则该行为是未定义的.原文:
The binary operator % yields the remainder of the division of the first operand by the second (after usual arithmetic conversions). If the quotient
a/b
is representible in the result type, (a/b)*b + a%b == a. If the second operand is zero, the behavior is undefined.- 如果一个或两个操作数为负,其余的符号是实现定义的,因为它依赖于舍入方向的整数除法(至 C++11)原文:if one or both operands are negative, the sign of the remainder is implementation-defined, as it depends on the rounding direction of integer division (至 C++11)
#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'; }
输出:
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
[编辑] 按位逻辑运算符
对于每一个对促进整数类型
L
和R
下面的函数签名参与重载解析:原文:
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) |
||
LR
是通常的算术转换L
和R
的结果原文:
where
LR
is the result of usual arithmetic conversions on L
and R
由于操作〜是按位NOT(1的补数)参数值(晋升后)。操作的结果和操作数的按位“与”价值(通常的算术转换后)。结果运算符
原文:
是按位OR值的操作数(通常的算术转换后)。运算符^是按位异或操作数的值(通常的算术转换后)的结果
#include <iostream> int main() { std::cout << std::hex << std::showbase; uint16_t mask = 0x00f0; uint32_t a = 0x12345678; std::cout << "Value: " << a << " mask: " << mask << '\n' << "Setting bits: " << (a
输出:
Value: 0x12345678 mask: 0xf0 Setting bits: 0x123456f8 Clearing bits: 0x12345608 Selecting bits: 0x70
[编辑] 移位运算符
对于每一个对促进整数类型
L
和R
,下面的函数签名参与重载解析原文:
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) |
||
内置的操作数的移位运算符为整数类型或无作用域的枚举类型。积分促销活动进行评估前两个操作数。返回类型是左操作数的类型后固有领土优惠.
原文:
The operands of the built-in bitwise shift operators have either integral types or unscoped enumeration type. Integral promotions are performed on both operands before evaluation. The return type is the type of the left operand after integal promotions.
对于无符号的
的价值,降低模极大值的返回类型加1,按位左移位,得到转移的目标类型的位被丢弃。签署
,如果它是表示的返回类型,否则该行为是未定义的.
a
,a << b
a * 2b的价值,降低模极大值的返回类型加1,按位左移位,得到转移的目标类型的位被丢弃。签署
a
,a << b
a * 2b,如果它是表示的返回类型,否则该行为是未定义的.
原文:
For unsigned
, reduced modulo maximum value of the return type plus 1 (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). For signed
if it is representable by the return type, otherwise the behavior is undefined.
a
, the value of a << b
is the value of a * 2b, reduced modulo maximum value of the return type plus 1 (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). For signed
a
, the value of a << b
is a * 2bif it is representable by the return type, otherwise the behavior is undefined.
对于无符号的
的整数部分。对于负
a
签署a
与非负值,值a >> b
a/2b的整数部分。对于负
a
,a >> b
是实现定义的(在大多数实现中,执行算术右移,这样的结果仍然是阴性)原文:
For unsigned
. For negative
a
and for signed a
with nonnegative values, 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)在任何情况下,如果将右操作数的值是负的,或大于或等于在促进的左操作数的数目的比特,该行为是未定义.
原文:
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.
#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'; }
输出:
0x123 << 1 = 0x246 0x123 << 63 = 0x8000000000000000 0x10 << 10 = 0x4000 -1000 >> 1 = -500
[编辑] 标准库
算术运算符重载,许多标准库类型.
原文:
Arithmetic operators are overloaded for many standard library types.
[编辑] 一元算术运算符
实现一元+和一元 - (公共成员函数of std::chrono::duration )
| |
一元运算符复数 原文: applies unary operators to complex numbers (函数模板) | |
适用于一元算术运算符的valarray的每个元素 原文: applies a unary arithmetic operator to each element of the valarray (公共成员函数of std::valarray )
|
[编辑] 加法运算符
修改的时间点,由给定的持续时间 原文: modifies the time point by the given duration (函数模板) | |
连接两个字符串或一个字符串和一个字符 原文: concatenates two strings or a string and a char (函数模板) | |
进步的迭代器 (公共成员函数of std::reverse_iterator )
| |
减少了迭代器 (公共成员函数of std::reverse_iterator )
| |
进步的迭代器 (公共成员函数of std::move_iterator )
| |
减少了迭代器 (公共成员函数of std::move_iterator )
| |
进行复数算术,两个复杂的值或一个复杂的和一个标量 原文: performs complex number arithmetics on two complex values or a complex and a scalar (函数模板) | |
二元运算符的每个元素的两个valarrays,或valarray的和值 原文: applies binary operators to each element of two valarrays, or a valarray and a value (函数模板) |
[编辑] 乘法运算符
实现了算术运算的时间作为参数 原文: implements arithmetic operations with durations as arguments (函数模板) | |
进行复数算术,两个复杂的值或一个复杂的和一个标量 原文: performs complex number arithmetics on two complex values or a complex and a scalar (函数模板) | |
二元运算符的每个元素的两个valarrays,或valarray的和值 原文: applies binary operators to each element of two valarrays, or a valarray and a value (函数模板) |
[编辑] 按位逻辑运算符
执行二进制AND,OR,XOR和NOT 原文: performs binary AND, OR, XOR and NOT (公共成员函数of std::bitset )
| |
进行二值逻辑操作位集 原文: performs binary logic operations on bitsets (函数) | |
适用于一元算术运算符的valarray的每个元素 原文: applies a unary arithmetic operator to each element of the valarray (公共成员函数of std::valarray )
| |
二元运算符的每个元素的两个valarrays,或valarray的和值 原文: applies binary operators to each element of two valarrays, or a valarray and a value (函数模板) |
[编辑] 移位运算符
二元运算符的每个元素的两个valarrays,或valarray的和值 原文: applies binary operators to each element of two valarrays, or a valarray and a value (函数模板) | |
执行二进制左移和右移 原文: performs binary shift left and shift right (公共成员函数of std::bitset )
|
[编辑] 流插入/拔出运营商
在整个标准库,移位运算符通常是超负荷的I / O流(std::ios_base&或从它派生的类之一)的左操作数和返回类型。这些运营商被称为“流插入”和“流提取”运营商
原文:
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:
提取物格式的数据 (公共成员函数of std::basic_istream )
| |
提取字符和字符数组 原文: extracts characters and character arrays (函数模板) | |
插入格式化的数据 (公共成员函数of std::basic_ostream )
| |
插入字符数据 (函数) | |
序列化和反序列化一个复杂的数 原文: serializes and deserializes a complex number (函数模板) | |
执行流的输入和输出的位集 原文: performs stream input and output of bitsets (函数) | |
执行流I / O字符串 (函数模板) | |
执行流的输入和输出的伪随机数的发动机 原文: performs stream input and output on pseudo-random number engine (函数) | |
执行流输入和输出的伪随机数分布 原文: performs stream input and output on pseudo-random number distribution (函数) |
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||