C ++检测模板类 [英] C++ detect templated class

查看:182
本文介绍了C ++检测模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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<> 
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;
};
/ pre>

如果你想通常检查它是否为 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 if T is a std::map<A,B> or std::unordered_map<A,B> and both A and B be std::string. So basically check enables compile-time checking of the type T. 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 only map<A,B> and not map<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 or std::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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆