2353. std::next is over-constrained

Section: 27.4.3 [iterator.operations] Status: C++17 Submitter: Eric Niebler Opened: 2013-12-24 Last modified: 2017-09-10

Priority: 4

View all other issues in [iterator.operations].

View all issues with C++17 status.

Discussion:

In LWG 1011, std::next and std::prev were changed from accepting InputIterator to accepting ForwardIterator. 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<InputRange>>
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 at c++std-lib-35313 for 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 issue 1011 sought to address. I make no such attempt, nor do I believe it to be necessary.)

Suggested resolution:

Back out the resolution of 1011.

[Lenexa 2015-05-07: Move to Ready]

Proposed resolution:

This wording is relative to N3797.

  1. Change 27.3 [iterator.synopsis], header <iterator> synopsis, and 27.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);