std::forward_iterator
Defined in header <iterator>
|
||
template<class I> concept forward_iterator = |
(since C++20) | |
This concept refines std::input_iterator by requiring that I
also model std::incrementable
(thereby making it suitable for multi-pass algorithms), and guaranteeing that two iterators to the same range can be compared against each other.
Iterator concept determination
Definition of this concept is specified via an exposition-only alias template /*ITER_CONCEPT*/.
In order to determinate /*ITER_CONCEPT*/<I>, let ITER_TRAITS<I> denote I if the specialization std::iterator_traits<I> is generated from the primary template, or std::iterator_traits<I> otherwise:
- If ITER_TRAITS<I>::iterator_concept is valid and names a type, /*ITER_CONCEPT*/<I> denotes the type.
- Otherwise, if ITER_TRAITS<I>::iterator_category is valid and names a type, /*ITER_CONCEPT*/<I> denotes the type.
- Otherwise, if std::iterator_traits<I> is generated from the primary template, /*ITER_CONCEPT*/<I> denotes std::random_access_iterator_tag.
- Otherwise, /*ITER_CONCEPT*/<I> does not denote a type and results in a substitution failure.
Semantic requirements
I
models std::forward_iterator
if, and only if I
models all the concepts it subsumes, and given objects i
and j
of type I
:
- Comparison between iterators
i
andj
has a defined result if
-
i
andj
are iterators to the same underlying sequence, or - both
i
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.
- If
i
andj
are dereferenceable, they offer the multi-pass guarantee, that is:
-
i == j
implies++i == ++j
, and - ((void)[](auto x){ ++x; }(i), *i) is equivalent to
*i
.
-
Equality preservation
An expression is equality preserving if it results in equal outputs given equal inputs.
- The inputs to an expression consist of its operands.
- The outputs of an expression consist of its result and all operands modified by the expression (if any).
In specification of standard concepts, operands are defined as the largest subexpressions that include only:
- an id-expression, and
- invocations of std::move, std::forward, and std::declval.
The cv-qualification and value category of each operand is determined by assuming that each template type parameter denotes a cv-unqualified complete non-array object type.
Every expression required to be equality preserving is further required to be stable: two evaluations of such an expression with the same input objects must have equal outputs absent any explicit intervening modification of those input objects.
Unless noted otherwise, every expression used in a requires-expression is required to be equality preserving and stable, and the evaluation of the expression may only modify its non-constant operands. Operands that are constant must not be modified.
See also
(C++20) |
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) |