2012-05-23 25 views
6

मुझे टेम्पलेट मेटा-प्रोग्रामिंग के बारे में कुछ सीखने में रूचि है। नीचे दिए गए कोड में मैं कुछ टेम्पलेट रिकर्सन का उपयोग करके, संकलित समय पर निर्दिष्ट एन बिट्स को पकड़ने के लिए पर्याप्त एक हस्ताक्षरित अभिन्न प्रकार खोजने का प्रयास कर रहा हूं।सी ++ रिकर्सिव टेम्पलेट प्रकार कटौती

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

मैं निम्नलिखित मिल तनाव मुक्त होने के '। क्या कोई मुझे सही दिशा दिखा सकता है?

नोट: मैं जीसीसी 4.6 का उपयोग कर रहा हूं।

संपादित करें:
मैं एक पोस्ट है कि मैं याद किया पाया से पहले:
Automatically pick a variable type big enough to hold a specified number

कौन सा बढ़ावा में एक जवाब (जहां वे हमेशा से रहे हैं) के लिए कहते हैं:
boost_integer

यह मेरी व्यावहारिक आवश्यकता और बौद्धिक जिज्ञासा दोनों को हल करना चाहिए।

+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

संबंधित मुद्दे