C++ 運算符的優先級和關聯性表
- 1. Precedence and associativity (優先級和結合性)
- 2. Alternative spellings (替代拼寫)
- 3. C++ operator precedence and associativity table (C++ 運算符的優先級和關聯性表)
- References
C++ documentation (C++ 文檔)
https://learn.microsoft.com/en-us/cpp/cpp/
https://learn.microsoft.com/zh-cn/cpp/cpp/
C++ built-in operators, precedence, and associativity (C++ 內置運算符、優先級和關聯性)
https://learn.microsoft.com/en-us/cpp/cpp/cpp-built-in-operators-precedence-and-associativity
https://learn.microsoft.com/zh-cn/cpp/cpp/cpp-built-in-operators-precedence-and-associativity
The C++ language includes all C operators and adds several new operators. Operators specify an evaluation to be performed on one or more operands.
C++ 語言包括所有 C 運算符,并添加多個新的運算符。運算符指定對一個或多個操作數執行的計算。
1. Precedence and associativity (優先級和結合性)
Operator precedence specifies the order of operations in expressions that contain more than one operator. Operator associativity specifies whether, in an expression that contains multiple operators with the same precedence, an operand is grouped with the one on its left or the one on its right.
運算符優先級指定了包含多個運算符的表達式中的運算順序。運算符關聯性指定了在包含多個具有相同優先級的運算符的表達式中,操作數是與其左側還是右側的操作數組合。
precedence [?pres?d?ns]:n. 優先,優先權
associativity [?'s?????t?v?t?]:n. 可結合性,結合體,締合性
2. Alternative spellings (替代拼寫)
C++ specifies alternative spellings for some operators. In C, the alternative spellings are provided as macros in the <iso646.h>
header. In C++, these alternatives are keywords, and use of <iso646.h>
or the C++ equivalent <ciso646>
is deprecated.
C++ 為某些運算符指定了替代拼寫。在 C 中,替代拼寫在 <iso646.h>
標頭中作為宏提供。在 C++ 中,這些替代項是關鍵字,<iso646.h>
或 C++ 等效的 <ciso646>
已棄用。
3. C++ operator precedence and associativity table (C++ 運算符的優先級和關聯性表)
The following table shows the precedence and associativity of C++ operators (from highest to lowest precedence). Operators with the same precedence number have equal precedence unless another relationship is explicitly forced by parentheses.
下表顯示 C++ 運算符的優先級和關聯性 (從最高優先級到最低優先級)。優先級別編號相同的運算符具有等同的優先級別,除非由括號顯式施加另一種關系。
Operator Description | Operator | Alternative (替代項) |
---|---|---|
Group 1 precedence, no associativity (第 1 組優先級,無關聯性) | ||
Scope resolution (作用域解析運算符) | :: | |
Group 2 precedence, left to right associativity (第 2 組優先級,從左到右關聯) | ||
Member selection (object or pointer) | . or -> | |
Array subscript (數組下標) | [] | |
Function call (函數調用) | () | |
Postfix increment (后綴遞增) | ++ | |
Postfix decrement (后綴遞減) | -- | |
Type name (類型名稱) | typeid | |
Constant type conversion (常量類型轉換) | const_cast | |
Dynamic type conversion (動態類型轉換) | dynamic_cast | |
Reinterpreted type conversion (重新解釋的類型轉換) | reinterpret_cast | |
Static type conversion (靜態類型轉換) | static_cast | |
Group 3 precedence, right to left associativity (第 3 組優先級,從右到左關聯) | ||
Size of object or type (對象或類型的大小) | sizeof | |
Prefix increment (前綴遞增) | ++ | |
Prefix decrement (前綴遞減) | -- | |
One’s complement (二進制反碼) | ~ | compl |
Logical not (邏輯“非”) | ! | not |
Unary negation (一元求反) | - | |
Unary plus (一元加) | + | |
Address-of | & | |
Indirection (間接尋址) | * | |
Create object (創建對象) | new | |
Destroy object (銷毀對象) | delete | |
Cast (強制轉換) | () | |
Group 4 precedence, left to right associativity (第 4 組優先級,從左到右關聯) | ||
Pointer-to-member (objects or pointers) | .* or ->* | |
Group 5 precedence, left to right associativity (第 5 組優先級,從左到右關聯) | ||
Multiplication (乘法) | * | |
Division (除法) | / | |
Modulus (取模) | % | |
Group 6 precedence, left to right associativity (第 6 組優先級,從左到右關聯) | ||
Addition (加法) | + | |
Subtraction (減法) | - | |
Group 7 precedence, left to right associativity (第 7 組優先級,從左到右關聯) | ||
Left shift () | << | |
Right shift () | >> | |
Group 8 precedence, left to right associativity (第 8 組優先級,從左到右關聯) | ||
Less than (小于) | < | |
Greater than (大于) | > | |
Less than or equal to (小于或等于) | <= | |
Greater than or equal to (大于或等于) | >= | |
Group 9 precedence, left to right associativity (第 9 組優先級,從左到右關聯) | ||
Equality (等于) | == | |
Inequality (不相等) | != | not_eq |
Group 10 precedence left to right associativity (第 10 組優先級,從左到右關聯) | ||
Bitwise AND (按位“與”) | & | bitand |
Group 11 precedence, left to right associativity (第 11 組優先級,從左到右關聯) | ||
Bitwise exclusive OR (按位“異或”) | ^ | xor |
Group 12 precedence, left to right associativity (第 12 組優先級,從左到右關聯) | ||
Bitwise inclusive OR (按位“與或”) | | | bitor |
Group 13 precedence, left to right associativity (第 13 組優先級,從左到右關聯) | ||
Logical AND (邏輯與) | && | and |
Group 14 precedence, left to right associativity (第 14 組優先級,從左到右關聯) | ||
Logical OR (邏輯或) | || | or |
Group 15 precedence, right to left associativity (第 15 組優先級,從右到左關聯) | ||
Conditional (條件) | ? : | |
Assignment | = | |
Multiplication assignment (乘法賦值) | *= | |
Division assignment (除法賦值) | /= | |
Modulus assignment (取模賦值) | %= | |
Addition assignment (加法賦值) | += | |
Subtraction assignment (減法賦值) | -= | |
Left-shift assignment (左移賦值) | <<= | |
Right-shift assignment (右移賦值) | >>= | |
Bitwise AND assignment (按位“與”賦值) | &= | and_eq |
Bitwise inclusive OR assignment (按位“與或”賦值) | |= | or_eq |
Bitwise exclusive OR assignment (按位“異或”賦值) | ^= | xor_eq |
throw expression | throw | |
Group 16 precedence, left to right associativity (第 16 組優先級,從左到右關聯) | ||
Comma | , |
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] C++ built-in operators, precedence, and associativity, https://learn.microsoft.com/en-us/cpp/cpp/cpp-built-in-operators-precedence-and-associativity