Issue 2353: std::next is over-constrained (original) (raw)
This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.
2353. std::next
is over-constrained
Section: 24.4.3 [iterator.operations] Status: C++17 Submitter: Eric Niebler Opened: 2013-12-24 Last modified: 2017-09-07
Priority: 4
View other active issues in [iterator.operations].
View all other issues in [iterator.operations].
View all issues with C++17 status.
Discussion:
In LWG 1011(i), std::next
and std::prev
were changed from accepting InputIterator
to acceptingForwardIterator
. This needlessly excludes perfectly legitimate use cases. Consider the following hypothetical range-based implementation of drop, which creates a view of a range without the first n
elements:
template<typename Distance, typename InputRange> iterator_range<range_iterator_t> drop(Distance n, InputRange& rng) { return make_iterator_range( std::next(std::begin(rng), n), std::end(rng) ); }
I believe this to be a legitimate use case that is currently outlawed by the standard without cause. See the discussion beginning atc++std-lib-35313for an in-depth discussion of the issue, in which Howard Hinnant agreed that it was a defect.
(Some discussion then ensued about whether an overload should be added that only accepts rvalue InputIterators
to avoid the surprise that issue1011(i) sought to address. I make no such attempt, nor do I believe it to be necessary.)
Suggested resolution:
Back out the resolution of 1011(i).
[Lenexa 2015-05-07: Move to Ready]
Proposed resolution:
This wording is relative to N3797.
- Change 24.2 [iterator.synopsis], header
<iterator>
synopsis, and 24.4.3 [iterator.operations]before p.6 as indicated:template <class
ForwardInputIterator>
ForwardInputIterator next(ForwardInputIterator x,
typename std::iterator_traits<ForwardInputIterator>::difference_type n = 1);