본문 바로가기

프로그래밍 언어/C++

floating-integral conversion에 대해

반응형

conversion의 설계 개념

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

floating-integral conversions

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

floating-integral conversionsprvalue category에서 prvalue category로 변환을 정의한다.

floating-point typeprvalue categoryinteger typeprvalue category로 변경할 수 있다. conversion 결과는 fraction part를 버리는 truncated value다. truncated value를 목적 타입으로 표현할 수 없다면 undefined behavior다.

integer type이나 unscoped enumeration typeprvalue categoryfloating-point typeprvalue category로 변경할 수 있다. 정확한 값으로 변환할 수 있다면 conversion 결과는 정확한 값이고, 근접한 두 값 사이값이면, implementation-defined에 따라 두 값 중 하나를 선택한 값이다. 표현할 수 있는 범위를 벗어난 값에 대한 conversion 결과는 undefined behavior다. conversion 결과가 정확한 값이 아니면, 정밀도 손실이 발생한다.

목적 타입이 bool type인 경우 boolean conversion를 적용한다.
소스 타입이 bool type인 경우 false는 0, true는 1로 변경된 후 목적 타입으로 변경될 수 있다.

반응형