2015-03-30 12 views
7

मैं एक प्रकार विशेषता यदि किसी विशेष प्रकार किसी वर्ग टेम्पलेट का एक उदाहरण है की जाँच करता है कि है:यूनिफाई प्रकार और गैर प्रकार टेम्प्लेट पैरामीटर

template <template <typename...> class C, typename T> 
struct check_is_instance_of : std::false_type { }; 

template <template <typename...> class C, typename ...Ts> 
struct check_is_instance_of<C, C<Ts...>> : std::true_type { }; 

template <template <typename...> class C, typename T> 
struct is_instance_of : check_is_instance_of<C, std::remove_cv_t<T>> { }; 

दुर्भाग्य से, यह गैर प्रकार टेम्पलेट मापदंडों के लिए काम नहीं करता है क्योंकि वे विविध टेम्पलेट पैरामीटर द्वारा "कब्जा" नहीं हैं, इसलिए

is_instance_of<std::integral_constant, std::true_type> 

संकलन-त्रुटि उत्पन्न करता है। is_instance_of के कार्यान्वयन को लिखने का कोई तरीका है जो मनमाने ढंग से प्रकार और गैर-प्रकार के टेम्पलेट पैरामीटर के लिए काम करता है?

+0

नहीं आपके प्रश्न का उत्तर है, लेकिन यह कार्यान्वयन टेम्पलेट टेम्पलेट उपनामों के साथ काम नहीं करेगा। हो सकता है कि आप यही चाहते हैं, लेकिन शायद नहीं। – TartanLlama

उत्तर

1

मुझे नहीं लगता कि ऐसा करने का एक साफ तरीका है जब तक गैर-प्रकार के तर्क एक ही प्रकार के होते हैं और आप जानते हैं कि यह किस प्रकार का है। उस बहुत ही विशिष्ट मामले में, फ़ंक्शन ओवरलोडिंग का उपयोग किया जा सकता है।

किसी भी अन्य मामले में, आप सही अग्रेषण समस्या के टेम्पलेट-तर्क संस्करण में समाप्त होते हैं जहां आपको हर प्रकार/नॉनटाइप तर्क संयोजन के लिए विशेषज्ञता प्राप्त करनी होगी।

यदि आपको केवल सजातीय गैर-टेम्पलेट तर्कों को संबोधित करने की आवश्यकता है और आप प्रकार का अनुमान लगा सकते हैं, तो निम्न कार्य करना चाहिए। आप विभिन्न प्रकार के लिए (केवल पूर्णांक यहाँ कवर किया जाता है) instance_of ओवरलोड सकते हैं, लेकिन आप स्पष्ट रूप से प्रत्येक प्रकार आप को संभालने के लिए सक्षम होना चाहते हैं के लिए एक उदाहरण बनाने के लिए चाहते हैं:

// variation for non-type parameters, only for uniform parameters with 
// known type. 
template <typename V, template <V...> class C, typename T> 
struct check_is_instance_of_nontype : std::false_type { }; 

template <typename V, template <V...> class C, V... Values> 
struct check_is_instance_of_nontype<V, C, C<Values...>> : std::true_type { }; 

// this is as in your example 
template <template <typename...> class C, typename T> 
struct check_is_instance_of : std::false_type { }; 

template <template <typename...> class C, typename ...Ts> 
struct check_is_instance_of<C, C<Ts...>> : std::true_type { }; 

template <template <typename...> class C, typename T> 
struct is_instance_of : check_is_instance_of<C, std::remove_cv_t<T>> { }; 

template <template <typename...> class C, typename T> 
constexpr bool instance_of() 
{ 
    return is_instance_of< C, T>::value; 
} 

template <template <int...> class C, typename T> 
constexpr bool instance_of() 
{ 
    return check_is_instance_of_nontype< int, C, T>::value; 
} 

template<int...> 
struct Duck 
{ 
}; 

template<typename A, typename B> 
struct Swallow 
{ 

}; 

int main() { 
    typedef Duck<1, 2> SittingDuck; 
    typedef Swallow< int, int> UnladenSwallow; 

    std::cout << instance_of< Duck, SittingDuck>() << instance_of< Swallow, UnladenSwallow>(); 
    return 0; 
} 
संबंधित मुद्दे