C++

C++ 연산자 우선순위

최고관리자
2018.04.17 09:08 1,906 0

본문

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)타입 캐스트
*역참조
&주소값
sizeofSize-of 연산자
newnew[]동적 메모리 할당
deletedelete[]동적 메모리 해제
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 연산 후 할당
16throw(예외를 위한)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&band *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)=cbecause 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_caststatic_castdynamic_castreinterpret_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
assignmentincrement
decrement
arithmeticlogicalcomparisonmember
access
other

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

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

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

!a
&& b
|| b

== b
!= b
< b
> b
<= b
>= 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)



출처 : http://ko.cppreference.com/w/cpp/language/operator_precedence

댓글목록 0

등록된 댓글이 없습니다.