, 당신은 여러 단계로 구축 된 특성이 포함 할 수 있습니다. 우리는 비트 형식으로 정수형을 매핑하는 것으로 시작합니다 :
#define CREATE_SIGNED_META_OBJ(x) template <>\
struct signed_integer_type<x> {\
typedef int##x##_t type;\
};
#define CREATE_UNSIGNED_META_OBJ(x) template <>\
struct unsigned_integer_type<x> {\
typedef uint##x##_t type;\
};
template <std::size_t length>
struct signed_integer_type;
template <std::size_t len>
struct unsigned_integer_type;
CREATE_SIGNED_META_OBJ(8)
CREATE_SIGNED_META_OBJ(16)
CREATE_SIGNED_META_OBJ(32)
CREATE_SIGNED_META_OBJ(64)
CREATE_UNSIGNED_META_OBJ(8)
CREATE_UNSIGNED_META_OBJ(16)
CREATE_UNSIGNED_META_OBJ(32)
CREATE_UNSIGNED_META_OBJ(64)
그런 다음 특성을 자체적으로 작성할 수 있습니다. 우리는
template <typename Int, bool>
struct get_smallest_for_max_plus_one;
template <typename Int>
struct get_smallest_for_max_plus_one<Int, true> {
typedef typename signed_integer_type<2*sizeof(Int)*8>::type type;
};
template <typename Int>
struct get_smallest_for_max_plus_one<Int, false> {
typedef typename unsigned_integer_type<sizeof(Int)*8>::type type;
};
template <typename Int>
using get_fittest_int_type = get_smallest_for_max_plus_one<Int, std::numeric_limits<Int>::min() == 0>;
지금 우리가 어떤 통합 정수와 직접
get_fittest_int_type
을 사용할 수 있습니다 ... 주장
std::numeric_limits<Int>::min() == 0
의 값에 우리의 이분법을 적용 할 ... 실행 예
Coliru에서 찾을 수 있습니다. 하지만 일관성을 위하여
, 내가 원하는 작업 ... signed
또는 unsigned
속성을 유지하기 위해이 경우, 당신은 단지 다음과 같이 get_smallest_for_max_plus_one
의 전문화를 대체 할 수 원하는 같아요
// Unsigned type, we want to get the smallest unsigned type that can hold max + 1
template <typename Int>
struct get_smallest_for_max_plus_one<Int, true> {
typedef typename unsigned_integer_type<2*sizeof(Int)*8>::type type;
};
// Signed type, we want the smallest signed type that can hold max + 1
template <typename Int>
struct get_smallest_for_max_plus_one<Int, false> {
typedef typename signed_integer_type<2*sizeof(Int)*8>::type type;
};
ammm을 .. 'T '의 두 배 크기 인 부호없는 정수형이 아닌가? –
@ NathanOliver OP는 그 값을 유지할 수있는 모든 유형 중 가장 작은 것을 의미한다고 생각합니다. 즉 uint32_t와 uint64_t 중 하나를 선택하면 uint32_t를 선택하십시오. – Borgleader
@Borgleader 의미가 있습니다. 감사. – NathanOliver