이름공간
변수
행위

C++ 연산자 우선순위

cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
operators
operator precedence
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

아래의 테이블은 C++ 연산자들의 연결규칙과 우선순위입니다. 연산자는 위에서부터 아래로 내림차순의 우선순위를 가집니다.

The following table lists the precedence and associativity of C++ operators. Operators are listed top to bottom, in descending precedence.

우선순위 연산자 설명 결합방향
1 :: Scope resolution 좌 → 우
2 ++ -- 후위 증가와 감소
() 함수호출
[] 배열 첨자
. 참조로 요소 선택
-> 포인터를 통해 요소 선택
3 ++ -- 전위 증가와 감소 우 → 좌
+ 단항 덧셈, 뺄셈
! ~ 논리 NOT, 비트단위 NOT
(type) 타입 캐스트
* 역참조
& 주소값
sizeof Size-of 연산자
new, new[] 동적 메모리 할당
delete, delete[] 동적 메모리 해제
4 .* ->* 멤버 접근 좌 → 우
5 * / % 곱셈, 나눗셈, 나머지
6 + 더하기, 빼기
7 << >> 비트 왼쪽 쉬프트와 오른쪽 쉬프트
8 < <= 관계 연산자 < 와 ≤
> >= 관계 연산자 > 와 ≥
9 == != 관계 = 와 ≠
10 & 비트 AND
11 ^ 비트 XOR (exclusive or)
12 | 비트 OR (inclusive or)
13 && 논리 AND
14 || 논리 OR
15 ?: 삼항연산자 우 → 좌
= 직접 할당 (C++ 클래스를 위해 기본 제공)
+= −= 합과 차 할당
*= /= %= 곱, 몫, 나머지 할당
<<= >>= 비트 왼쪽 쉬프트와 오른쪽 쉬프트 후 할당
&= ^= |= 비트연산 AND, XOR, OR 연산 후 할당
16 throw (예외를 위한)Throw 연산자
17 , 콤마 좌 → 우


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)++.

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 우 → 좌 associativity.

An operator's precedence is unaffected by overloading.

[편집] Notes

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

const_cast, static_cast, dynamic_cast, reinterpret_cast and typeid are not included since they are never ambiguous.

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

[편집] See also

Order of evaluation of operator arguments at run time.

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)