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:
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.
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.
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. |
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 |
Name | Expression | Type requirements | Return 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. |
Return Category | std::return_category<X>::type | A type convertible to std::writable_iterator_tag |
Name | Expression | Return type |
---|---|---|
Dereference assignment | *x = a | unspecified |
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.
Return Category | std::return_category<X>::type | A type convertible to std::swappable_iterator_tag |
Name | Expression | Return type |
---|---|---|
Iterator Swap | std::iter_swap(x, y) | void |
Dereference and Swap | std::swap(*x, *y) | void |
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 |
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 |
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 |
Name | Expression | Type requirements | Return type |
---|---|---|---|
Preincrement | ++i | X& | |
Postincrement | i++ | convertible to const X& |
Traversal Category | std::traversal_category<X>::type | A type convertible to std::bidirectional_traversal_tag |
Name | Expression | Type requirements | Return type |
---|---|---|---|
Predecrement | --i | X& | |
Postdecrement | i-- | convertible to const X& |
Traversal Category | std::traversal_category<X>::type | A type convertible to std::random_access_traversal_tag |
Name | Expression | Type 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) |