2012-05-23 6 views
6

템플릿 메타 프로그래밍에 대해 조금 배우는 데 관심이 있습니다. 아래의 코드에서는 컴파일 타임에 지정된 N 비트를 유지하기에 충분한 크기의 부호없는 정수형을 찾고 일부 템플릿 재귀를 사용하려고합니다.C++ 재귀 템플릿 유형 공제

그것은 내 재귀처럼 보인다
error: no matching function for call to ‘FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2(int)’ 
note: candidates are: 
note: constexpr FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2() 
note: candidate expects 0 arguments, 1 provided 
note: constexpr FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2(const FindIntegralType2<15u, NextIntegralType<unsigned char> >&) 
note: no known conversion for argument 1 from ‘int’ to ‘const FindIntegralType2<15u, NextIntegralType<unsigned char> >&’ 

'는되지 않습니다 :

template <typename T> 
struct NextIntegralType 
{ 
}; 

template <> 
struct NextIntegralType<void> 
{ 
    typedef unsigned char type; 
}; 

template <> 
struct NextIntegralType<unsigned char> 
{ 
    typedef unsigned short type; 
}; 

...More type 'iteration' here... 

template<size_t BITS, typename T> 
struct FindIntegralType2 
{ 
    typedef std::conditional<BITS <= sizeof(typename T::type)*8, T, FindIntegralType2<BITS, NextIntegralType<typename T::type>>> _type; 
    typedef typename _type::type type; 
}; 

template<size_t BITS> 
struct FindIntegralType 
{ 
    typedef typename FindIntegralType2<BITS, NextIntegralType<void>>::type type; 
}; 

내가 변수를 선언하고 그것에 정수 값을 할당

...

FindIntegralType<15>::type test(4000); 

나는 다음을 얻을 푸는 '. 누구든지 올바른 방향으로 나를 가리킬 수 있습니까?

참고 : GCC 4.6을 사용하고 있습니다.

편집 : 나는 내가 놓친 게시물을 발견
전에 :
boost_integer

: (그들은 항상) 향상에 답변을 가리키는
Automatically pick a variable type big enough to hold a specified number

이것은 나의 실제적인 필요와 지적 호기심을 모두 해결해야합니다.

+0

왜 stdint.h를 사용하지 않습니까? – mfontanini

+0

@mfontanini 좀 더 자세히 설명해 주시겠습니까? – TractorPulledPork

답변

2

_type::typestd::conditional<...>::type이 아니라 FindIntegralType2<...>::type이됩니다. 이 값을 typedef typename _type::type::type type; (너무 많이 type x_X)으로 변경하십시오. 이렇게하면 문제가 해결됩니다.

+0

올바른 내용입니다. 고맙습니다! – TractorPulledPork