Częściowa specjalizacja, gdy chcesz, aby umożliwić dowolną porównawczej hasher, na klucz równej referencyjnymi i przydzielania:
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;
};
Jeśli chcesz sprawdzić, czy T
używał domyślnej wersji tych typów (tylko map<A,B>
, a nie map<A,B,my_comp>
, można pominąć argumenty szablonu i przejść do wyraźnej specjalizacji:
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;
};
A jeśli chcesz ogólnie sprawdzić, czy jest to std::map
lub std::unordered_map
dowolnej kombinacji klucz/wartość (i komparator/hasher/itd.), Można go całkowicie generic pobrane z 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>>{};