C ++检测模板类 [英] C++ detect templated class
问题描述
template< typename T>
struct check
{
static const bool value = false;
};
我想要做的是让 check< T>
为 std :: map< A,B>
或 std :: unordered_map
,并且 A
和 code> be
std :: string
。因此,基本上 check
启用对 T
类型的编译时检查。
我如何做这个?
当你想允许任何比较器,哈希,等比较器和分配器:
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
这些类型的默认版本(也只有地图< A,B>
而不是地图< A,B,my_comp>
,您可以省略模板参数并使用明确的专门化:
template<>
/ pre>
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
,你可以完全通用此处:#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<模板,模板< 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>
template<typename T> struct check { static const bool value = false; };
What I want to do is to have
check<T>::value
be true if and only ifT
is astd::map<A,B>
orstd::unordered_map<A,B>
and bothA
andB
bestd::string
. So basicallycheck
enables compile-time checking of the typeT
. How do I do this?解决方案Partial specialization for when you want to allow any comparator, hasher, key-equal-comparator and allocator:
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; };
If you want to check if
T
used the default version of those types (aka onlymap<A,B>
and notmap<A,B,my_comp>
, you can omit the template arguments and go with explicit specialization: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; };
And if you want to generally check if it's a
std::map
orstd::unordered_map
of any key/value combination (and comparator / hasher / etc.), you can go fully generic as taken from 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>>{};
这篇关于C ++检测模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!