Is the traversal category of boost::iterator_adaptor determined by the category of the adapted iterator?

Viewed 111

My class inheriting from boost::iterator_adaptor has code like

private:

using is_bidirectional = std::is_convertible<
  iterator_category, std::bidirectional_iterator_tag>;

typename std::enable_if<is_bidirectional::value>::type
decrement() { ... }

and I am wondering if the enable_if part is redundant. As written, the decrement() overload will not be enabled if the adapted iterator is not bidirectional. However I do not know whether boost::iterator_adaptor already uses SFINAE such that I do not need to be concerned about the traversal category in the private overloads. The code compiles and works just fine. I am only asking whether the enable_if part is useful or whether it is redundant.

1 Answers

Yes.

Indeed, the adapter, by default, does mirror the base-iterator's category.

Here's a comprehensive test that works:

Live On Coliru

#include <boost/iterator_adaptors.hpp>

template <typename BaseIterator>
struct Adapt : boost::iterator_adaptor<Adapt<BaseIterator>, BaseIterator> {
    using base = boost::iterator_adaptor<Adapt<BaseIterator>, BaseIterator>;
    using base::base;
    using base::operator=;
};

#include <iostream>
#include <iterator>
#include <typeinfo>

template <typename It>
std::string cat(It const&) {
    return typeid(typename std::iterator_traits<It>::iterator_category).name();
}

template <typename It, typename OutIt>
void test(std::string caption, It f, It l, OutIt out) {
    Adapt<It> af{f}, al{l};

    std::cout << caption << ", adapts to " << cat(af) << "\t";
    std::copy(af, al, out);
    std::cout << "\n";
}

template <typename Container, typename OutIt>
void test(std::string caption, Container const& c, OutIt out) {
    using BaseIterator = typename Container::const_iterator;
    Adapt<BaseIterator> f{c.begin()}, l{c.end()};

    test(caption, f, l, out);
}

#include <vector>
#include <forward_list>
#include <list>
#include <sstream>

int main() {
    using V = std::vector<int>;
    using L = std::list<int>;
    using FL = std::forward_list<int>;
    using II = std::istream_iterator<int>;
    using OI = std::ostream_iterator<int>;

    static_assert(std::is_same<std::random_access_iterator_tag, std::iterator_traits<V::iterator>::iterator_category>{}, "");
    static_assert(std::is_same<std::bidirectional_iterator_tag, std::iterator_traits<L::iterator>::iterator_category>{}, "");
    static_assert(std::is_same<std::forward_iterator_tag, std::iterator_traits<FL::iterator>::iterator_category>{}, "");
    static_assert(std::is_same<std::input_iterator_tag, std::iterator_traits<II>::iterator_category>{}, "");
    static_assert(std::is_same<std::output_iterator_tag, std::iterator_traits<OI>::iterator_category>{}, "");

    OI out(std::cout, " ");
    test("vector      ", V  { 1,2,3 }, out);
    test("list        ", L  { 4,5,6 }, out);
    test("forward_list", FL { 7,8,9 }, out);
    {
        std::istringstream iss("10 11 12");
        II f(iss), l;
        test("stream input", f, l, out);
    }
}

Which prints¹

vector      , adapts to std::random_access_iterator_tag 1 2 3 
list        , adapts to std::bidirectional_iterator_tag 4 5 6 
forward_list, adapts to std::forward_iterator_tag   7 8 9 
stream input, adapts to std::input_iterator_tag 10 11 12 

¹ filtered the output through c++filt -t for prettier type info

Related