std::input_iterator_tag, std::output_iterator_tag, std::forward_iterator_tag, std::bidirectional_iterator_tag, std::random_access_iterator_tag, std::contiguous_iterator_tag

< cpp‎ | iterator
Iterator library
Iterator concepts
Iterator primitives
(deprecated in C++17)
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
Range access
Defined in header <iterator>
struct input_iterator_tag { };
struct output_iterator_tag { };
struct forward_iterator_tag : public input_iterator_tag { };
struct bidirectional_iterator_tag : public forward_iterator_tag { };
struct random_access_iterator_tag : public bidirectional_iterator_tag { };
struct contiguous_iterator_tag: public random_access_iterator_tag { };
(6) (since C++20)

Defines the category of an iterator. Each tag is an empty type.


[edit] Iterator category

For every LegacyIterator type It, a typedef std::iterator_traits<It>::iterator_category must be defined to be an alias to one of these tag types, to indicate the most specific category that It is in.

  1. input_iterator_tag corresponds to LegacyInputIterator.
  2. output_iterator_tag corresponds to LegacyOutputIterator.
  3. forward_iterator_tag corresponds to LegacyForwardIterator.
  4. bidirectional_iterator_tag corresponds to LegacyBidirectionalIterator.
  5. random_access_iterator_tag corresponds to LegacyRandomAccessIterator.

Iterator category tags carry information that can be used to select the most efficient algorithms for the specific requirement set that is implied by the category.

Iterator concept

For every input_iterator type It, either It::iterator_concept (if std::iterator_traits<It> is generated from primary template) or std::iterator_traits<It>::iterator_concept (if std::iterator_traits<It> is specialized) may be declared as an alias to one of these tags, to indicate the strongest iterator concept that It intends to model.

  1. input_iterator_tag corresponds to input_iterator.
  2. forward_iterator_tag corresponds to forward_iterator.
  3. bidirectional_iterator_tag corresponds to bidirectional_iterator.
  4. random_access_iterator_tag corresponds to random_access_iterator.
  5. contiguous_iterator_tag corresponds to contiguous_iterator.

If iterator_concept is not provided, iterator_category is used as a fallback. If iterator_category is not provided either (i.e. It is not a LegacyIterator), and std::iterator_traits<It> is not specialized, random_access_iterator_tag is assumed.

In any case, each concept is not satisfied if the required operations are not supported, regardless of the tag.

(since C++20)

[edit] Notes

There is no separate tag for LegacyContiguousIterator. That is, it is not possible to tell a LegacyContiguousIterator based on its iterator_category. To define specialized algorithm for contiguous iterators, use the contiguous_iterator concept. (since C++20)

[edit] Example

Common technique for algorithm selection based on iterator category tags is to use a dispatcher function (the alternative is std::enable_if)

#include <iostream>
#include <vector>
#include <list>
#include <iterator>
// quite often implementation details are hidden in a dedicated namespace
namespace implementation_details {
template<class BDIter>
void alg(BDIter, BDIter, std::bidirectional_iterator_tag)
    std::cout << "alg() called for bidirectional iterator\n";
template<class RAIter>
void alg(RAIter, RAIter, std::random_access_iterator_tag)
    std::cout << "alg() called for random-access iterator\n";
} // namespace implementation_details
template<class Iter>
void alg(Iter first, Iter last)
    implementation_details::alg(first, last,
        typename std::iterator_traits<Iter>::iterator_category());
int main()
    std::vector<int> v;
    alg(v.begin(), v.end());
    std::list<int> l;
    alg(l.begin(), l.end());
//    std::istreambuf_iterator<char> i1(std::cin), i2;
//    alg(i1, i2); // compile error: no matching function for call


alg() called for random-access iterator
alg() called for bidirectional iterator

[edit] See also

(deprecated in C++17)
base class to ease the definition of required types for simple iterators
(class template) [edit]
provides uniform interface to the properties of an iterator
(class template) [edit]