Search code examples
c++iteratorgeneric-programming

What is an example of a c++ forwarditerator?


I am looking for an example of an iterator which satisfies the limited qualities to be considered a ForwardIterator, while not being anything else in the hierarchy of iterators.

I have written a program which prints which of the five iterator categories an iterator conforms to. I have tested it for the other four iterator categories.


Solution

  • A typical example is std::forward_list<T>::iterator. Testing snippet:

    #include <iostream>
    #include <iterator>
    #include <forward_list>
    #include <vector>
    
    template<typename T>
    bool test = std::is_same <
                    typename std::iterator_traits <
                        typename T::iterator
                    >::iterator_category,
                    std::forward_iterator_tag
                >::value ;
    
    int main()
    {
        // test wheter std::forward_list's iterator is a forward_iterator
        std::cout << std::boolalpha << test<std::forward_list<int>> << std::endl;
        // what about std::vector?
        std::cout << std::boolalpha << test<std::vector<int>>;
    }
    

    Live on Coliru