Teil Spezialisierung, wenn Sie jeden Komparator, Hasher, Schlüssel equal-Komparator und allocator zulassen mögen:
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;
};
Wenn Sie, wenn überprüfen mögen T
verwendet die Standardversion dieser Typen (aka nur map<A,B>
und nicht map<A,B,my_comp>
, können Sie die Vorlage Argumente weglassen und gehen mit expliziter Spezialisierung:
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;
};
Und wenn Sie in der Regel überprüfen wollen, ob es ein std::map
oder std::unordered_map
jeden Schlüssel/Wert-Kombination (und Vergleicher/Hasher/etc.) ist, können Sie sich ganz generic gehen, wie aus here genommen:
#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>>{};