operator precedence

Download c++ eBook

Remarks

Operators are listed top to bottom, in descending precedence. Operators with the same number have equal precedence and the same associativity.

  1. ::
  2. The postfix operators: [] () T(...) . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid
  3. The unary prefix operators: ++ -- * & + - ! ~ sizeof new delete delete[]; the C-style cast notation, (T)...; (C++11 and above) sizeof... alignof noexcept
  4. .* and ->*
  5. *, /, and %, binary arithmetic operators
  6. + and -, binary arithmetic operators
  7. << and >>
  8. <, >, <=, >=
  9. == and !=
  10. &, the bitwise AND operator
  11. ^
  12. |
  13. &&
  14. ||
  15. ?: (ternary conditional operator)
  16. =, *=, /=, %=, +=, -=, >>=, <<=, &=, ^=, |=
  17. throw
  18. , (the comma operator)

The assignment, compound assignment, and ternary conditional operators are right-associative. All other binary operators are left-associative.

The rules for the ternary conditional operator are a bit more complicated than simple precedence rules can express.

  • An operand binds less tightly to a ? on its left or a : on its right than to any other operator. Effectively, the second operand of the conditional operator is parsed as though it is parenthesized. This allows an expression such as a ? b , c : d to be syntactically valid.
  • An operand binds more tightly to a ? on its right than to an assignment operator or throw on its left, so a = b ? c : d is equivalent to a = (b ? c : d) and throw a ? b : c is equivalent to throw (a ? b : c).
  • An operand binds more tightly to an assignment operator on its right than to : on its left, so a ? b : c = d is equivalent to a ? b : (c = d).

Related Examples

Stats

69 Contributors: 6
Saturday, May 6, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook