Namespaces
Variants
Actions

std::iterator_traits

From cppreference.com
< cpp‎ | iterator
 
 
Iterator library
Iterator concepts
Iterator primitives
(deprecated in C++17)
iterator_traits


Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
template< class Iter >
struct iterator_traits;
template< class T >
struct iterator_traits<T*>;
template< class T >
struct iterator_traits<const T*>;
(removed in C++20)

std::iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIterator types. This makes it possible to implement algorithms only in terms of iterators.

The template can be specialized for user-defined iterators so that the information about the iterator can be retrieved even if the type does not provide the usual typedefs.

User specializations may define the nested type iterator_concept to one of iterator category tags, to indicate conformance to the iterator concepts.

(since C++20)

Contents

[edit] Template parameters

Iter - the iterator type to retrieve properties for

[edit] Member types

Nested type Definition
difference_type Iter::difference_type
value_type Iter::value_type
pointer Iter::pointer
reference Iter::reference
iterator_category Iter::iterator_category


If Iter does not have any of the five nested types above, then this template has no members by any of those names (std::iterator_traits is SFINAE-friendly).

(since C++17)
(until C++20)

If Iter does not have pointer, but has all four remaining nested types, then these four nested types are declared as follows:

Nested type Definition
difference_type Iter::difference_type
value_type Iter::value_type
pointer void
reference Iter::reference
iterator_category Iter::iterator_category


Otherwise, if Iter satisfies the exposition-only concept __LegacyInputIterator, the nested types are declared as follows:

Nested type Definition
difference_type std::incrementable_traits<Iter>::difference_type
value_type std::indirectly_readable_traits<Iter>::value_type
pointer
  • Iter::pointer if valid.
  • Otherwise decltype(std::declval<Iter&>().operator->()) if valid.
  • Otherwise void.
reference
iterator_category
  • Iter::iterator_category if valid.
  • Otherwise,