C++ Operator Precedence
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
下表列出了C + +运算符的优先级和结合性。从上到下,运算符的优先级下降.
原文:
The following table lists the precedence and associativity of C++ operators. Operators are listed top to bottom, in descending precedence.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | ::
|
Scope resolution | Left-to-right |
2 | ++ --
|
Suffix/postfix increment and decrement | |
()
|
Function call | ||
[]
|
Array subscripting | ||
.
|
Element selection by reference | ||
−>
|
Element selection through pointer | ||
3 | ++ --
|
Prefix increment and decrement | Right-to-left |
+ −
|
Unary plus and minus | ||
! ~
|
Logical NOT and bitwise NOT | ||
(type)
|
Type cast | ||
*
|
Indirection (dereference) | ||
&
|
Address-of | ||
sizeof
|
Size-of | ||
new , new[]
|
Dynamic memory allocation | ||
delete , delete[]
|
Dynamic memory deallocation | ||
4 | .* ->*
|
Pointer to member | Left-to-right |
5 | * / %
|
Multiplication, division, and remainder | |
6 | + −
|
Addition and subtraction | |
7 | << >>
|
Bitwise left shift and right shift | |
8 | < <=
|
For relational operators < and ≤ respectively | |
> >=
|
For relational operators > and ≥ respectively | ||
9 | == !=
|
For relational = and ≠ respectively | |
10 | &
|
Bitwise AND | |
11 | ^
|
Bitwise XOR (exclusive or) | |
12 | |
|
Bitwise OR (inclusive or) | |
13 | &&
|
Logical AND | |
14 | ||
|
Logical OR | |
15 | ?:
|
Ternary conditional | Right-to-left |
=
|
Direct assignment (provided by default for C++ classes) | ||
+= −=
|
Assignment by sum and difference | ||
*= /= %=
|
Assignment by product, quotient, and remainder | ||
<<= >>=
|
Assignment by bitwise left shift and right shift | ||
&= ^= |=
|
Assignment by bitwise AND, XOR, and OR | ||
16 | throw
|
Throw operator (for exceptions) | |
17 | ,
|
Comma | Left-to-right |
解析表达式时,操作员是列出的一些行会的约束更严格的(如果用括号),其参数比任何操作上列出它下面的一排。例如,表达式std::cout<<a&b和*p++被解析为(std::cout<<a)&b*(p++),而不是作为std::cout<<(a&b)或(*p)++.
原文:
When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expressions std::cout<<a&b and *p++ are parsed as (std::cout<<a)&b and *(p++), and not as std::cout<<(a&b) or (*p)++.
算子是在同一小区(可能有几行中列出的一个单元格的运营商),在给定的方向上具有相同的优先级的评价。例如,表达a=b=c被解析为a=(b=c),而不是从右到左结合性,因为(a=b)=c.
原文:
Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is parsed as a=(b=c), and not as (a=b)=c because of right-to-left associativity.
一个操作符的优先级是超载的影响.
原文:
An operator's precedence is unaffected by overloading.
[编辑] 注释
该标准本身并没有指定优先级。他们从该语法派生.
原文:
The standard itself doesn't specify precedence levels. They are derived from the grammar.
const_cast,static_cast,dynamic_cast,reinterpret_cast和typeid不包括在内,因为他们是绝不含糊.....
原文:
const_cast, static_cast, dynamic_cast, reinterpret_cast and typeid are not included since they are never ambiguous.
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||