This concept refinesstd::input_iterator by requiring thatI
also modelstd::incrementable (thereby making it suitable for multi-pass algorithms), and guaranteeing that two iterators to the same range can be compared against each other.
[edit]Iterator concept determination
Definition of this concept is specified via an exposition-only alias template/*ITER_CONCEPT*/.
In order to determine/*ITER_CONCEPT*/<I>, letITER_TRAITS<I> denoteI if the specializationstd::iterator_traits<I> is generated from the primary template, orstd::iterator_traits<I> otherwise:
- IfITER_TRAITS<I>::iterator_concept is valid and names a type,/*ITER_CONCEPT*/<I> denotes the type.
- Otherwise, ifITER_TRAITS<I>::iterator_category is valid and names a type,/*ITER_CONCEPT*/<I> denotes the type.
- Otherwise, ifstd::iterator_traits<I> is generated from the primary template,/*ITER_CONCEPT*/<I> denotesstd::random_access_iterator_tag.
(That is,std::derived_from</*ITER_CONCEPT*/<I>,std::forward_iterator_tag> is assumed to betrue.) - Otherwise,/*ITER_CONCEPT*/<I> does not denote a type and results in a substitution failure.
[edit]Semantic requirements
I
modelsstd::forward_iterator
if, and only ifI
models all the concepts it subsumes, and given objectsi andj of typeI
:
- Comparison between iteratorsi andj has a defined result if
- i andj are iterators to the same underlying sequence, or
- bothi andj are value-initialized, in which case they compare equal.
- Pointers and references obtained from a forward iterator into a range remain valid while the range exists.
- Ifi andj are dereferenceable, they offer themulti-pass guarantee, that is:
- i== j implies++i==++j, and
- ((void)[](auto x){++x;}(i),*i) is equivalent to*i.
Unlike theLegacyForwardIterator requirements, theforward_iterator
concept does not require dereference to return a reference.
[edit]Example
A minimum forward iterator.
#include <cstddef>#include <iterator> class SimpleForwardIterator{public:using difference_type=std::ptrdiff_t;using value_type=int; SimpleForwardIterator(); SimpleForwardIterator(const SimpleForwardIterator&); SimpleForwardIterator& operator=(const SimpleForwardIterator&); int operator*()const; SimpleForwardIterator& operator++(); SimpleForwardIterator operator++(int){auto tmp=*this;++*this;return tmp;} bool operator==(const SimpleForwardIterator&)const;}; static_assert(std::forward_iterator<SimpleForwardIterator>);
[edit]See also
| specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented (concept)[edit] |
| specifies that aforward_iterator is a bidirectional iterator, supporting movement backwards (concept)[edit] |