Created on 2024-02-25.00:00:00 last changed 9 months ago
Proposed resolution:
This wording is relative to N4971.
Modify [iterator.operations] as indicated:
template<class InputIterator> constexpr typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);-4-Preconditions: last is reachable from first, or InputIterator meets the Cpp17RandomAccessIterator requirements and first is reachable from last. The return type can represent the result.
-5- Effects: If InputIterator meets the Cpp17RandomAccessIterator requirements, returns (last - first); otherwise, increments first until last is reached and returns the number of increments.
[ 2024-03-12; Jonathan provides improved wording ]
[ 2024-03-12; Reflector poll ]
Set priority to 4 after reflector poll.
The proposed change is wrong, the new wording only associates with the second
condition, but should also apply when "last
is reachable from
first
".
This wording is relative to N4971.
Modify [iterator.operations] as indicated:
template<class InputIterator> constexpr typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);-4-Preconditions: last is reachable from first, or InputIterator meets the Cpp17RandomAccessIterator requirements,
-5- Effects: If InputIterator meets the Cpp17RandomAccessIterator requirements, returns (last - first); otherwise, increments first until last is reached and returns the number of increments.andfirst is reachable from last , and typename iterator_traits<InputIterator>::difference_type can represent the result of this function call.
std::distance for random access iterators is defined in terms of (last - first) ([iterator.operations] p5) for Cpp17RandomAccessIterators.
However, this subtraction is not guaranteed to be well-defined ([random.access.iterators], Table [tab:randomaccessiterator]):
Table 91: Cpp17RandomAccessIterator requirements (in addition to Cpp17BidirectionalIterator) [tab:randomaccessiterator] Expression Return type Operational semantics Assertion/note
pre-/post-condition[…] b - a difference_type return n; Preconditions: there exists a
value n of type difference_type
such that a + n == b.
b == a + (b - a).
For example, pointer subtraction is undefined if the result isn't representable as std::ptrdiff_t, and user-defined types with random access iterators aren't required to have a difference which is always representable by difference_type.
std::distance(&a, &b) can't be well-defined when &b - &a is not, so std::distance is missing a precondition.History | |||
---|---|---|---|
Date | User | Action | Args |
2024-03-12 14:05:02 | admin | set | messages: + msg14003 |
2024-03-12 14:05:02 | admin | set | messages: + msg14002 |
2024-02-25 15:57:50 | admin | set | messages: + msg13967 |
2024-02-25 00:00:00 | admin | create |