C++ 运算符优先级

转载 2015年11月18日 00:44:11

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
type()   type{} Functional cast
() Function call
[] Subscript
.   -> Member access
3 ++   -- Prefix increment and decrement Right-to-left
+   - Unary plus and minus
!   ~ Logical NOT and bitwise NOT
(type) C-style cast
* Indirection (dereference)
& Address-of
sizeof Size-of[note 1]
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 operators = and ≠ respectively
10 & Bitwise AND
11 ^ Bitwise XOR (exclusive or)
12 | Bitwise OR (inclusive or)
13 && Logical AND
14 || Logical OR
15 ?: Ternary conditional[note 2] Right-to-left
throw throw operator
= Direct assignment (provided by default for C++ classes)
+=   -= Compound assignment by sum and difference
*=   /=   %= Compound assignment by product, quotient, and remainder
<<=   >>= Compound assignment by bitwise left shift and right shift
&=   ^=   |= Compound assignment by bitwise AND, XOR, and OR
16 , Comma Left-to-right
  1.  The operand of sizeof can't be a C-style type cast: the expression sizeof (int) * p is unambiguously interpreted as(sizeof(int)) * p, but not sizeof((int)*p).
  2.  The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored.

When parsing an expression, an operator which is listed on some row of the table above with a precedence will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it with a lower precedence. For example, the expressions std::cout << a & b and *p++ are parsed as (std::cout << a) & b and *(p++), and not as std::cout << (& b) or (*p)++.

Operators that have the same precedence are bound to their arguments in the direction of their associativity. For example, the expression = b = c is parsed as = (= c), and not as (= b) = c because of right-to-left associativity of assignment, but + b - c is parsed (+ b) - c and not + (- c) because of left-to-right associativity of addition and subtraction.

Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left (delete ++*p is delete(++(*p))) and unary postfix operators always associate left-to-right (a[1][2]++ is ((a[1])[2])++). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a.(b++))

Operator precedence is unaffected by operator overloading.

Notes

Precedence and associativity are compile-time concepts and are independent from order of evaluation, which is a runtime concept.

The standard itself doesn't specify precedence levels. They are derived from the grammar.

const_caststatic_castdynamic_castreinterpret_casttypeidsizeof...noexcept and alignof are not included since they are never ambiguous.

Some of the operators have alternate spellings (e.g., and for &&or for ||not for !, etc.).

Relative precedence of the ternary conditional and assignment operators differs between C and C++: in C, assignment is not allowed on the right-hand side of a ternary conditional operator, so = a < d ? a++ : a = dcannot be parsed. Many C compilers use a modified grammar where ?: has higher precedence than =, which parses that as = ( ((< d) ? (a++) : a) = d ) (which then fails to compile because ?: is never lvalue in C and =requires lvalue on the left). In C++, ?: and = have equal precedence and group right-to-left, so that = a < d ? a++ : a = d parses as = ((< d) ? (a++) : (= d)).

C++ 运算符的优先级和结合性

运算符的优先级和结合性 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 后置++ 后置自增运算符 变量名++ 左到右 后置--...
  • lfb_2048
  • lfb_2048
  • 2017年03月14日 12:01
  • 2054

c++ 运算符优先级

原文地址:http://en.cppreference.com/w/cpp/language/operator_precedence C++ Operator Precedence ...
  • yuyanggo
  • yuyanggo
  • 2015年09月29日 11:02
  • 512

Linux C/C++ 运算符:种类、优先级、结合性

一、C语言运算符优先级 详细列表 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] ...
  • kanguolaikanguolaik
  • kanguolaikanguolaik
  • 2015年04月14日 22:47
  • 1334

C++中的运算符和运算符优先级总结

原文链接:http://www.jb51.net/article/83857.htm 运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。C++ 内置了丰富的运算符,并提供了以下类型的运算符: ...
  • rioalian
  • rioalian
  • 2017年07月22日 15:34
  • 78

C语言中++自增运算符后置时的使用说明(附C语言运算符优先级表)

自增运算符后置情形下的探索。
  • ds1231h
  • ds1231h
  • 2015年11月30日 00:54
  • 2149

C 语言运算符优先级(记忆口诀)

优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括...
  • u013630349
  • u013630349
  • 2015年08月12日 15:09
  • 7301

c/c++系列的运算符优先级总结

经常写程序的时候,遇到运算符优先级的问题,令我汗颜的是,查书的次数挺多的%26hellip;%26hellip;狠狠心,总结下。不过还要结合大量的编程实践来深入脑海。 1、首先永远忘不了的是,逗号运...
  • j4ws8qvs
  • j4ws8qvs
  • 2014年11月04日 07:57
  • 184

javascript基础(赋值运算符,关系运算符,相等运算符,三元运算符,运算符的优先级,代码块)(十)

有代码编写基础的,自学时请略过 1.赋值运算符: /* 赋值运算符可以将一个值赋值给一个变量 = - =可以...
  • u010853130
  • u010853130
  • 2017年02月08日 09:35
  • 419

JavaScript 操作符(算术/逻辑/比较/赋值运算符/优先级/自增自减/三目运算符)

1. 算术运算符 + (加):相加或连接 - (减)     *  (乘)  /  (除) % (取余) ( ) (提高优先级) 高级运算对象:Math(平方、立方、三角函数、……) ...
  • carriehaohao
  • carriehaohao
  • 2017年03月04日 22:11
  • 874

逻辑运算符、位运算符、移位运算符、三目运算符、运算符的优先级

一、逻辑运算符 :逻辑运算符的作用是用于连接布尔表达式的。 1、& (与,并且) 规律: 只有左右变量同时为true,那么结果才是true,否则就false。2、| (或,或者) 规律: 只要...
  • qq_34944851
  • qq_34944851
  • 2016年09月22日 11:00
  • 1997
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C++ 运算符优先级
举报原因:
原因补充:

(最多只允许输入30个字)