【问题标题】:Type traits to match pointer to collections键入特征以匹配指向集合的指针
【发布时间】:2014-10-11 07:33:39
【问题描述】:

我正在编写一个 SFINAE 匹配类,它可以匹配指向集合类型的指针。

我们目前有 std::is_pointer,我已经写了:

// SFINAE test for const_iterator for member type
template <typename T>
class has_const_iterator{
private:
    typedef char True;
    typedef long False;

    template <typename C> static True test(typename C::const_iterator*) ;
    template <typename C> static False test(...);

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

如何在 std::enable_if 中同时使用 std::is_pointer 和 has_const_iterator 或者如何编写可以匹配指向集合类型的指针的新类型特征?谢谢。

【问题讨论】:

    标签: c++ templates c++11 sfinae typetraits


    【解决方案1】:
    template<class T>
    struct is_pointer_to_collection 
         : std::integral_constant<bool, std::is_pointer<T>::value 
               && has_const_iterator<typename std::remove_pointer<T>::type>::value> {};
    

    Demo.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      相关资源
      最近更新 更多