Apache C++ Standard Library Reference Guide

## __iterator_category()

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Tag Types
- See Also

### Local Index

No Entries

### Summary

An iterator primitive that determines the category to which an iterator belongs.

> NOTE -- This function is now obsolete. It is retained here to support compilers that do not include partial specialization, but will be dropped in a subsequent release.

### Synopsis

    #include <iterator>
    template <class Category, class T, class Distance,
              class Pointer, class Reference>
    inline Category
             __iterator_category(const iterator<Category,
                                     T, Distance, Pointer,
                                     Reference>&);
    template <class T>
    inline random_access_iterator_tag __iterator_category(const T*);

### Description

The `__iterator_category()` family of function templates allows you to determine the category to which any iterator belongs. The first function takes an iterator of a specific type and returns the tag for that type. The last takes a `T*` and returns `random_access_iterator_tag`.

### Tag Types

    input_iterator_tag
    output_iterator_tag
    forward_iterator_tag
    bidirectional_iterator_tag
    random_access_iterator_tag

The `__iterator_category()` function is particularly useful for improving the efficiency of algorithms. An algorithm can use this function 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 returned from `__iterator_category()` to select from one of several different auxiliary functions. Because this is a compile time selection, use of this primitive incurs no significant runtime overhead.

`__iterator_category()` is typically used like this:

    template <class Iterator>
    void foo(Iterator start, Iterator finish)
    {
      __foo(begin,end,__iterator_category(start));
    }

    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

Other iterator primitives: `__distance_type()`, `distance()`, `advance()`, iterator