On this page
C++ named requirements: LegacyForwardIterator
A LegacyForwardIterator is a LegacyIterator that can read data from the pointed-to element.
Unlike LegacyInputIterator and LegacyOutputIterator, it can be used in multipass algorithms.
If a LegacyForwardIterator it originates from a Container, then it's value_type is the same as the container's, so dereferencing (*it) obtains the container's value_type.
Requirements
The type It satisfies LegacyForwardIterator if
- The type
Itsatisfies LegacyInputIterator - The type
Itsatisfies DefaultConstructible - Objects of the type
Itprovide multipass guarantee described below - Let
Tbe the value type ofIt. The type std::iterator_traits<It>::reference must be either
-
T&orT&&(since C++11) ifItsatisfies LegacyOutputIterator (Itis mutable), or- const T& or
const T&&(since C++11) otherwise (Itis constant),
-
(where
Tis 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 lvalue of typeItreference, 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 |
|---|---|---|
i++ |
It |
It ip = i; ++i; return ip; |
*i++ |
reference |
A mutable LegacyForwardIterator is a LegacyForwardIterator that additionally satisfies the LegacyOutputIterator requirements.
Multipass guarantee
Given a and b, dereferenceable iterators of type It:
- If
aandbcompare equal (a == bis contextually convertible totrue) then either they are both non-dereferenceable or*aand*bare references bound to the same object. - If
*aand*brefer to the same object, thena == b. - Assignment through a mutable
ForwardIteratoriterator cannot invalidate the iterator (implicit due toreferencedefined as a true reference). - Incrementing a copy of
adoes not change the value read froma(formally, eitherItis a raw pointer type or the expression(void)++It(a), *ais equivalent to the expression*a). a == bimplies++a == ++b.
Singular iteratorsA value-initialized LegacyForwardIterator behaves like the past-the-end iterator of some unspecified empty container: it compares equal to all value-initialized LegacyForwardIterators of the same type. |
(since C++14) |
ConceptFor the definition of
where the exposition-only concept |
(since C++20) |
Notes
Unlike the std::forward_iterator concept, the LegacyForwardIterator requirements requires dereference to return a reference.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 1212 (N3066) |
C++98 | the return type of *i++ did not match the returntype of *i-- required by LegacyBidirectionalIterator |
changed the return type to reference |
| LWG 1311 (N3066) |
C++98 | 'a == b implies ++a == ++b' alonedid not offer multipass guarantee[1] |
also requires 'a == bimplies ++a != b'[2] |
| LWG 3798 | C++20 | __LegacyForwardIterator requiredstd::iter_reference_t<It> to be an lvalue reference type |
also allows rvalue reference types |
- In the scenario where
aandbuse the same underlying iterator, evaluating the expression++a == ++bactually increments the underlying container twice, but the result is stilltrue. - Formally also requires implying
++b != a.
See also
|
(C++20)
|
specifies that an input_iterator is a forward iterator, supporting equality comparison and multi-pass (concept) |
| Iterator library | provides definitions for iterators, iterator traits, adaptors, and utility functions |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/named_req/ForwardIterator