जब आप किसी भी तुलनित्र, क़मी बनाने की मशीन, की-बराबर-तुलनित्र और संभाजक अनुमति देना चाहते हैं के लिए आंशिक विशेषज्ञता:
template<class Comp, class Alloc>
struct check<std::map<std::string, std::string, Comp, Alloc>>{
static const bool value = true;
};
template<class Hash, class KeyEq, class Alloc>
struct check<std::unordered_map<std::string, std::string, Hash, KeyEq, Alloc>>{
static const bool value = true;
};
आप अगर जाँच करना चाहते हैं T
ने उन प्रकारों के डिफ़ॉल्ट संस्करण का उपयोग किया (उर्फ केवल map<A,B>
और map<A,B,my_comp>
नहीं, आप टेम्पलेट तर्क छोड़ सकते हैं और स्पष्ट विशेषज्ञता के साथ जा सकते हैं:
template<>
struct check<std::map<std::string, std::string>>{
static const bool value = true;
};
template<>
struct check<std::unordered_map<std::string, std::string>>{
static const bool value = true;
};
और आप आम तौर पर जांच करने के लिए अगर यह एक std::map
या किसी कुंजी/मान संयोजन के std::unordered_map
(और तुलनित्र/क़मी बनाने की मशीन/आदि) है चाहते हैं तो आपको here से पूरी तरह से सामान्य रूप में लिया जाना कर सकते हैं:
#include <type_traits>
template < template <typename...> class Template, typename T >
struct is_specialization_of : std::false_type {};
template < template <typename...> class Template, typename... Args >
struct is_specialization_of< Template, Template<Args...> > : std::true_type {};
template<class A, class B>
struct or_ : std::integral_constant<bool, A::value || B::value>{};
template<class T>
struct check
: or_<is_specialization_of<std::map, T>,
is_specialization_of<std::unordered_map, T>>{};