.\" ident @(#)iterator_traits.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH iterator_traits 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2iterator_traits\fP \ - Returns basic information about an iterator. .SH SYNOPSIS .br template struct iterator_traits .br { .RE .RS 2 typedef typename Iterator::value_type value_type; .br typedef typename Iterator::difference_type .RE .RS 29 difference_type; .RE .RS 2 typedef typename Iterator::pointer pointer; .br typedef typename Iterator::reference reference; .br typedef typename Iterator::iterator_category .RE .RS 29 iterator_category; .RE .RS 0 }; .br .br // Specialization .br template struct iterator_traits .br { .RE .RS 2 typedef T value_type; .br typedef ptrdiff_t difference_type; .br typedef T* pointer; .br typedef T& reference; .br typedef random_access_iterator_tag iterator_category; .RE .RS 0 }; .SH 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 \f2value_type\fP, difference_type, \f2pointer\fP, \f2reference\fP, and \f2iterator_category\fP, 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 \f2iterator_category\fP 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 \f2advance\fP and \f2distance\fP primitives use \f2iterator_category\fP to maximize their efficiency by using the tag to select from one of several different auxiliary functions. The \f2iterator_category \fPmust therefore be one of the iterator tags included by the library. .SH TAG TYPES .br input_iterator_tag .br output_iterator_tag .br forward_iterator_tag .br bidirectional_iterator_tag .br random_access_iterator_tag \f2iterator_traits::iterator_category\fP is typically used like this: .br template .br void foo(Iterator first, Iterator last) .br { .RE .RS 1 __foo(begin,end, .RE .RS 7 iterator_traits::iterator_category); .RE .RS 0 } .br .br template .br void __foo(Iterator first, Iterator last, .RE .RS 10 input_iterator_tag> .RE .RS 0 { .RE .RS 2 // Most general implementation .RE .RS 0 } .br .br template .br void __foo(Iterator first, Iterator last, .RE .RS 10 bidirectional_iterator_tag> .RE .RS 0 { .RE .RS 2 // Implementation takes advantage of bi-directional .br // capability of the iterators .RE .RS 0 } .br .br ...etc. See the iterator section for a description of iterators and the capabilities associated with each type of iterator tag. .SH 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 \f2__distance_type\fP, \f2__value_type\fP, and \f2__iterator_category\fP 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 \f2std\fP. .SH SEE ALSO __value_type, __distance_type, __iterator_category, distance, advance, iterator