C++ 연산자 우선순위
아래의 테이블은 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.
일반 연산자 | ||||||
---|---|---|---|---|---|---|
대입 | 증가 감소 |
산술 | 논리 | 비교 | 멤버 접근 |
기타 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast 는 타입을 다른 호환 가능한 타입으로 변환합니다 |