C++ concepts: ForwardIterator

From cppreference.com
< cpp‎ | concept

A ForwardIterator is an Iterator that can read data from the pointed-to element.

Unlike InputIterator and OutputIterator, it can be used in multipass algorithms.


[edit] Requirements

The type It satisfies ForwardIterator if

  • The type It satisfies InputIterator
  • The type It satisfies DefaultConstructible
  • Objects of the type It provide multipass guarantee described below
  • The type std::iterator_traits<It>::reference must be exactly
  • T& if It satisfies OutputIterator (It is mutable)
  • const T& otherwise (It is constant),
(where T is the type denoted by std::iterator_traits<It>::value_type)
  • Equality and inequality comparison is defined over all iterators for the same underlying sequence and the value initialized-iterators (since C++14).

And, given

  • i, dereferenceable iterator of type It
  • reference, the type denoted by std::iterator_traits<It>::reference

The following expressions must be valid and have their specified effects

Expression Return type Equivalent expression Notes
i++ It It ip=i; ++i; return ip;
*i++ reference

A mutable ForwardIterator is a ForwardIterator that additionally satisfies the OutputIterator requirements.

[edit] Multipass guarantee

Given a and b, dereferenceable iterators of type It

  • If a and b compare equal (a == b is contextually convertible to true) then either they are both non-dereferenceable or *a and *b are references bound to the same object
  • Assignment through a mutable ForwardIterator iterator cannot invalidate the iterator (implicit due to reference defined as a true reference)
  • incrementing a copy of a does not change the value read from a (formally, either It is a raw pointer type or the expression (void)++It(a), *a is equivalent to the expression *a)
  • a == b implies ++a == ++b

Singular iterators

A value-initialized ForwardIterator behaves like the past-the-end iterator of some unspecified empty container: it compares equal to all value-initialized ForwardIterators of the same type.
(since C++14)

[edit] See also