yuefengz
yuefengz

Reputation: 3358

Type traits to match pointer to collections

I am writing a SFINAE matching class which can match a pointer to collection type.

We currently have std::is_pointer and I have written:

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

How can I use both std::is_pointer and has_const_iterator in a std::enable_if or how can I write a new type traits that can match a pointer to collection type? Thanks.

Upvotes: 1

Views: 193

Answers (1)

T.C.
T.C.

Reputation: 137425

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.

Upvotes: 5

Related Questions