Issue2353

Title 2353. std::next is over-constrained
Status open Section [iterator.operations]
Submitter Eric Niebler

Created on 2013-12-24.00:00:00 by admin, last changed by admin.

Messages
msg7429 (view) Date: 2015-05-22.19:14:31

Proposed resolution:

This wording is relative to N3797.

  1. Change [iterator.synopsis], header <iterator> synopsis, and [iterator.operations] before p.6 as indicated:

    template <class ForwardInputIterator>
      ForwardInputIterator next(ForwardInputIterator x,
        typename std::iterator_traits<ForwardInputIterator>::difference_type n = 1);
    
msg6788 (view) Date: 2015-05-22.19:14:31

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

msg6787 (view) Date: 2013-12-24.00:00:00

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.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> open
2015-10-27 16:52:45adminsetstatus: ready -> wp
2015-05-22 19:14:31adminsetstatus: new -> ready
2015-05-22 19:14:31adminsetmessages: + msg7429
2014-01-12 22:13:32adminsetmessages: + msg6788
2013-12-24 00:00:00admincreate