Apache C++ Standard Library Reference Guide

## iterator_traits

Library: Iterators

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Tag Types
- See Also
- Standards Conformance

### Local Index

No Entries

### Summary

Returns basic information about an iterator.

### Synopsis

    #include <iterator>

    namespace std {
      template <class Iterator>
      struct iterator_traits;
    }

### Description

The class template iterator_traits and its specializations allow algorithms to access information about a particular iterator in a uniform way. The template requires either an iterator with a basic interface consisting of the types `value_type`, `difference_type`, `pointer`, `reference`, and `iterator_category`, or it requires a specialization for the iterator. The library includes partial specializations to handle all pointer types.

iterator_traits are used within algorithms to create local variables of either the type pointed to by the iterator or of the iterator's distance type. The traits also improve the efficiency of algorithms by making use of knowledge about basic iterator categories provided by the `iterator_category` member. An algorithm can use this "tag" to select the most efficient implementation an iterator is capable of handling without sacrificing the ability to work with a wide range of iterator types. For instance, both the `advance()` and `distance()` primitives use `iterator_category` to maximize their efficiency by using the tag to select from one of several different auxiliary functions. The `iterator_category` must therefore be one of the iterator tags included by the library.

### Interface

    template <class Iterator>
    struct iterator_traits
    {
       typedef typename Iterator::value_type value_type;
       typedef typename Iterator::difference_type difference_type;
       typedef typename Iterator::pointer pointer;
       typedef typename Iterator::reference reference;
       typedef typename Iterator::iterator_category
                                  iterator_category;
    };

    // Specializations for pointers
    template <class T>
    struct iterator_traits<T*>
    {
       typedef T value_type;
       typedef ptrdiff_t difference_type;
       typedef value_type* pointer;
       typedef value_type& reference;
       typedef random_access_iterator_tag iterator_category;
    };

### Tag Types

    struct input_iterator_tag {};
    struct output_iterator_tag {};
    struct forward_iterator_tag: input_iterator_tag {};
    struct bidirectional_iterator_tag: forward_iterator_tag {};
    struct random_access_iterator_tag:
                         bidirectional_iterator_tag {};

`iterator_traits<>::iterator_category` is typically used like this:

    template <class Iterator>
    void foo(Iterator start, Iterator finish)
    {
      foo(begin,end,
            iterator_traits<Iterator>::iterator_category);
    }

    template <class Iterator>
    void foo(Iterator start, Iterator finish,
               input_iterator_tag>
    {
      // Most general implementation
    }

    template <class Iterator>
    void foo(Iterator start, Iterator finish,
               bidirectional_iterator_tag>
    {
      // Implementation takes advantage of bi-directional
      // capability of the iterators
    }

    ...etc.

See the iterator section for a description of iterators and the capabilities associated with each type of iterator tag.

### See Also

`__value_type()`, `__distance_type()`, `__iterator_category()`, `distance()`, `advance()`, iterator

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 24.3.1