C++ Boost

Iterator Concepts

The standard iterator categories and requirements are flawed because they use a single hierarchy of requirements to address two orthogonal issues: iterator traversal and dereference return type. The current iterator requirement hierarchy is mainly geared towards iterator traversal (hence the category names), while requirements that address dereference return type sneak in at various places.

The iterator requirements should be separated into two hierarchies. One set of concepts handles the return type semantics:

The other set of concepts handles iterator traversal: The current Input Iterator and Output Iterator requirements will continue to be used as is. Note that Input Iterator implies Readable Iterator and Output Iterator implies Writable Iterator.

Note: we considered defining a Single-Pass Iterator, which could be combined with Readable or Writable Iterator to replace the Input and Output Iterator requirements. We rejected this idea because there are some differences between Input and Output Iterators that make it hard to merge them: for example Input Iterator requires Equality Comparable while Output Iterator does not.

Figure 1: The iterator concepts and refinement relationships.

Relationship with the standard iterator concepts

std::Input Iterator implies boost::ReadableIterator.

std::Output Iterator implies boost::Writable Iterator.

std::Forward Iterator refines boost::Forward Iterator and boost::Constant Lvalue Iterator or boost::Mutable Lvalue Iterator.

std::Bidirectional Iterator refines boost::Bidirectional Iterator and boost::Constant Lvalue Iterator or boost::Mutable Lvalue Iterator.

std::Random Access Iterator refines boost::Random Access Iterator and boost::Constant Lvalue Iterator or boost::Mutable Lvalue Iterator.

Notation

X The iterator type.
T The value type of X, i.e., std::iterator_traits<X>::value_type.
x, y An object of type X.
t An object of type T.


Readable Iterator

A Readable Iterator is an iterator that dereferences to produce an rvalue that is convertible to the value_type of the iterator.

Associated Types

Value type std::iterator_traits<X>::value_type The type of the objects pointed to by the iterator.
Reference type std::iterator_traits<X>::reference The return type of dereferencing the iterator. This type must be convertible to T.
Return Category std::return_category<X>::type A type convertible to std::readable_iterator_tag

Refinement of

Copy Constructible

Valid expressions

NameExpressionType requirementsReturn type
Dereference *x   std::iterator_traits<X>::reference
Member access x->m T is a type with a member named m. If m is a data member, the type of m. If m is a member function, the return type of m.


Writable Iterator

A Writable Iterator is an iterator that can be used to store a value using the dereference-assignment expression.

Definitions

If x is an Writable Iterator of type X, then the expression *x = a; stores the value a into x. Note that operator=, like other C++ functions, may be overloaded; it may, in fact, even be a template function. In general, then, a may be any of several different types. A type A belongs to the set of value types of X if, for an object a of type A, *x = a; is well-defined and does not require performing any non-trivial conversions on a.

Associated Types

Return Category std::return_category<X>::type A type convertible to std::writable_iterator_tag

Refinement of

Copy Constructible

Valid expressions

NameExpressionReturn type
Dereference assignment *x = a unspecified


Swappable Iterator

A Swappable Iterator is an iterator whose dereferenced values can be swapped.

Note: the requirements for Swappable Iterator are dependent on the issues surrounding std::swap() being resolved. Here we assume that the issue will be resolved by allowing the overload of std::swap() for user-defined types.

Note: Readable Iterator and Writable Iterator combined implies Swappable Iterator because of the fully templated std::swap(). However, Swappable Iterator does not imply Readable Iterator nor Writable Iterator.

Associated Types

Return Category std::return_category<X>::type A type convertible to std::swappable_iterator_tag

Valid expressions

Of the two valid expressions listed below, only one OR the other is required. If std::iter_swap() is overloaded for X then std::swap() is not required. If std::iter_swap() is not overloaded for X then the default (fully templated) version is used, which will call std::swap() (this means changing the current requirements for std::iter_swap()).

NameExpressionReturn type
Iterator Swap std::iter_swap(x, y) void
Dereference and Swap std::swap(*x, *y) void


Constant Lvalue Iterator

A Constant Lvalue Iterator is an iterator that dereferences to produce a const reference to the pointed-to object, i.e., the associated reference type is const T&. Changing the value of or destroying an iterator that models Constant Lvalue Iterator does not invalidate pointers and references previously obtained from that iterator.

Refinement of

Readable Iterator

Associated Types

Reference type std::iterator_traits<X>::reference The return type of dereferencing the iterator, which must be const T&.
Return Category std::return_category<X>::type A type convertible to std::constant_lvalue_iterator_tag


Mutable Lvalue Iterator

A Mutable Lvalue Iterator is an iterator that dereferences to produce a reference to the pointed-to object. The associated reference type is T&. Changing the value of or destroying an iterator that models Mutable Lvalue Iterator does not invalidate pointers and references previously obtained from that iterator.

Refinement of

Readable Iterator, Writable Iterator, and Swappable Iterator.

Associated Types

Reference type std::iterator_traits<X>::reference The return type of dereferencing the iterator, which must be T&.
Return Category std::return_category<X>::type A type convertible to std::mutable_lvalue_iterator_tag


Forward Traversal Iterator

The Forward Iterator is an iterator that can be incremented. Also, it is permissible to make multiple passes through the iterator's range.

Refinement of

Copy Constructible, Assignable, Default Constructible, and Equality Comparable

Associated types

Difference Type std::iterator_traits<X>::difference_type A signed integral type used for representing distances between iterators that point into the same range.
Traversal Category std::traversal_category<X>::type A type convertible to std::forward_traversal_tag

Valid expressions

NameExpressionType requirements Return type
Preincrement ++i X&
Postincrement i++ convertible to const X&


Bidirectional Traversal Iterator

An iterator that can be incremented and decremented.

Refinement of

Forward Traversal Iterator

Associated types

Traversal Category std::traversal_category<X>::type A type convertible to std::bidirectional_traversal_tag

Valid expressions

NameExpressionType requirements Return type
Predecrement --i X&
Postdecrement i-- convertible to const X&


Random Access Traversal Iterator

An iterator that provides constant-time methods for moving forward and backward in arbitrary-sized steps.

Refinement of

Bidirectional Traversal Iterator and Less Than Comparable where < is a total ordering

Associated types

Traversal Category std::traversal_category<X>::type A type convertible to std::random_access_traversal_tag

Valid expressions

NameExpressionType requirements Return type
Iterator addition i += n X&
Iterator addition i + n or n + i X
Iterator subtraction i -= n X&
Iterator subtraction i - n X
Difference i - j std::iterator_traits<X>::difference_type
Element operator i[n] X must also be a model of Readable Iterator. std::iterator_traits<X>::reference
Element assignment i[n] = t X must also be a model of Writable Iterator. unspecified


Copyright © 2000 Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu)