当您想要允许任何比较器、哈希器、key-equal-comarator 和分配器时的部分专业化:
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>>{};