Boost logo

Boost :

Subject: Re: [boost] [type_traits] Interest in is_iterator type trait?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2014-08-20 08:24:53


On 19/08/14 19:09, pfultz2 wrote:
>
>> Fwiw, I used something closely related in libc++ for SFINAE on the “member
>> template members” of the containers (the ones taking iterators) to
>> disambiguate them from the members/constructors not taking iterators, but
>> having the same number of parameters. But instead of is_iterator, I
>> needed
>> is_input_iterator, is_forward_iterator, etc. These refinements are
>> implemented similar to what Beman shows, but takes into account which std
>> iterator tag the nested iterator_category type is implicitly convertible
>> to.
>
> I actually have used a `has_iterator_traversal` trait to check iterator
> traversal, like this:
>
> template<class T, class Traversal, class Enable = void>
> struct has_iterator_traversal
> : boost::mpl::bool_<false>
> {};
>
> template<class T, class Traversal>
> struct has_iterator_traversal<T, Traversal, typename
> boost::enable_if&lt;is_iterator&lt;T> >::type>
> : boost::is_convertible<typename boost::iterator_traversal<T>::type,
> Traversal>::type
> {};

Why not just write

template<class Iterator>
void my_function_impl(Iterator it, std::input_iterator_tag)
{
   ...
}

template<class Iterator>
void my_function(Iterator it)
{
   return my_function_impl(it, typename
std::iterator_traits<Iterator>::iterator_category());
}

It's simpler and it probably compiles faster too.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk