본문 바로가기

프로그래밍 언어/C++

integral promotion conversion에 대해

반응형

conversion의 설계 개념

C++의 conversion를 정의할 때, 직교하는 3 요소, value category, cv-qualification, data representation를 설계 개념으로 사용한다. conversion를 설계할 때, 요소 중 하나만을 변형하고, 다른 요소를 변형하지 않는다. 어떤 이유로, 예를 들어 하위 호환성을 위해, 설계 개념을 위반할 때는 스펙 문서는 관련된 특수 조항을 별도로 명시한다.

integer conversion rank

정수형 변수 타입간의 rank는 다음과 같다. char8_t, char16_t, char32_t, wchar_t 타입의 integer conversion rank는 underlying type의 integer conversion rank다. 같은 열에 있는 데이터 타입간 rank 우열을 결정할 수 없다. 일종의 partial ordering set이다.

integral promotions conversion

data representation 요소를 변형하기 위해 설계한 conversion이다. data representation를 변경하기 때문에, conversion rank로 분류된다. 또한 일반적으로 value category이나 cv-qualification를 변경하지 않는다.

integral promotions conversion는 source type의 모든 값을 표현할 수 있는 가장 작은 integer conversion rank를 갖고 있는 target type으로 변경할 수 있다. integral promotions conversion는 prvalue category에서 prvalue category로 변환을 정의한다.

이런 이유로 integer conversion rank 순서를 따라 변환 가능한 target 타입의 순서목록을 구성할 수 있다.
bit-field integral 타입에 대해서는 integral bit-field promotions 순서목록으로, bool 타입에 대해서는 bool promotions 순서목록으로, non-fixed unscoped enumeration 타입에 대해서는 non-fixed unscoped enumeration promotions 순서목록으로
그 외의 타입에 대해서는 integral promotions 순서목록으로 source type에 대응하는 target 타입을 결정한다.

scoped enumeration 타입에 대해 integral promotions conversion를 적용할 수 없다. 소스의 모든 값을 표현할 수 있는 방향으로 변환을 시도하기 때문에 무손실 변환이다.

integral promotions 순서목록:
(int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

integral bit-field promotions 순서목록:
(int, unsigned int)

non-fixed unscoped enumeration promotions 순서목록:
(int, unsigned int, long int, unsigned long int, long long int, unsigned long long int, signed extended integral type1, unsigned extended integral type1,...)

bool promotions 순서목록:
(int)

bool를 제외한 integral 타입

char8_t, char16_t, char32_t, wchar_t 타입의 prvalue category는 source 타입의 underlying type의 모든 값을 표현할 수 있는 integral promotions 순서목록의 첫 타입 prvalue category로 변경한다. 적절한 target 타입이 없다면, source 타입의 underlying type의 prvalue category로 변경한다.

integral bit-field 타입의 prvalue category는 모든 값을 표현할 수 있는 integral bit-field promotions 순서목록의 첫 타입 prvalue category로 변경한다. 적절한 target 타입이 없다면, integral promotions conversion를 적용할 수 없다.

bool 타입

bool 타입의 prvalue category는 bool promotions 순서목록에 따라 int 타입의 prvalue로 변경한다. 이때 false는 0으로, true는 1의 값을 갖는다.

unscoped enumeration 타입

non-fixed unscoped enumeration 타입의 prvalue category는 source 타입의 underlying type의 모든 값을 표현할 수 있는 non-fixed unscoped enumeration promotions 순서목록의 첫 타입 prvalue category로 변경한다.

fixed unscoped enumeration 타입의 prvalue category는 source 타입의 underlying type의 모든 값을 표현할 수 있는 integral promotions 순서목록의 첫 타입 prvalue category로 변경한다. 적절한 target 타입이 없다면, underlying type의 prvalue category로 변경한다.

unscoped enumeration bit-field 타입의 prvalue category는 bit-field가 없는 unscoped enumeration처럼 integral promotions conversion를 적용한다.

반응형