## iterator_traits

    Iterator traits class

- Summary
- Data Type and Member Function Indexes
- Synopsis
- Description
- Tag Types
- Warnings
- See Also

### Summary

Returns basic information about an iterator.

### Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

### Synopsis

    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;
    };

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

### Description

The iterator_traits template and specialization allows 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 one specialization (partial) to handle all pointer iterator 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.

### Tag Types

    input_iterator_tag
    output_iterator_tag
    forward_iterator_tag
    bidirectional_iterator_tag
    random_access_iterator_tag

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

    template <class Iterator>
    void foo(Iterator first, Iterator last)
    {
      __foo(begin,end,
            iterator_traits<Iterator>::iterator_category);
    }

    template <class Iterator>
    void __foo(Iterator first, Iterator last,
               input_iterator_tag>
    {
      // Most general implementation
    }

    template <class Iterator>
    void __foo(Iterator first, Iterator last,
               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.

### Warnings

If your compiler does not support partial specialization, then this template and specialization are not available to you. Instead you need to use the `__distance_type`, `__value_type`, and `__iterator_category` families of function templates. The Rogue Wave Standard C++ Library also includes alternate implementations of the distance, advance, and count functions when partial specialization is not supported by a particular compiler.

If your compiler does not support namespaces, then you do not need the using declaration for `std`.

### See Also

__value_type, __distance_type, __iterator_category, distance, advance, iterator

Copyright (c) 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998