2012-05-23 20 views
6

Şablon meta-programlama hakkında biraz bilgi edinmek istiyorum. Aşağıdaki kodda, bazı şablon özyineleme kullanarak derleme zamanında belirtilen N bitlerini tutacak kadar büyük imzasız bir integral türü bulmaya çalışıyorum.C++ özyinelemeli şablon indirimi

Benim özyineleme gibi görünüyor
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> >&’ 

'değildir:

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; 
}; 

Bir değişken bildirmek ve buna ayrılmaz bir değer atamak

...

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

aşağıdaki alıyorum gevşemek'. Beni doğru yöne yönlendiren var mı?

Not: GCC 4.6 kullanıyorum.

DÜZENLEME: Ben cevapsız bir yazı buldum
önce:
boost_integer

: (her zaman olduğu) artırımı bir cevap işaret
Automatically pick a variable type big enough to hold a specified number

Bu hem pratik ihtiyacım hem de entelektüel merakımı çözmeli.

+0

Neden stdint.h kullanıyorsunuz? – mfontanini

+0

@mfontanini Detaylandırır mısınız? – TractorPulledPork

cevap

2

Sorununuz, _type::type'un değerini değil, FindIntegralType2<...>::type değerini değerlendirdiğidir. typedef typename _type::type::type type; (çok fazla type x_X) olarak değiştirin. Bu probleminizi çözmeli.

+0

Bu doğru. Teşekkür ederim! – TractorPulledPork