C++ 运算符的优先级和关联性表

发布于:2024-07-05 ⋅ 阅读:(21) ⋅ 点赞:(0)

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


网站公告

今日签到

点亮在社区的每一天
去签到