Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounded next and prev should also return the distance advanced #629

Open
cjdb opened this issue Jul 26, 2019 · 0 comments
Open

Bounded next and prev should also return the distance advanced #629

cjdb opened this issue Jul 26, 2019 · 0 comments

Comments

@cjdb
Copy link
Contributor

cjdb commented Jul 26, 2019

This is only really necessary for the bounded overloads, but a uniform interface would be nice.

Consider this code, which is from some pseudo-C++ I wrote while trying to design a cmcstl2 implementation for stride_view's operator++.

auto step = next(current_, stride, last_);
back_step_ = current_ == last_ ? distance(current_, last_) : 0;

If decltype(last_) models sized_sentinel_for<decltype(current_)>, then everything is honky-dory, since it's an O(1) computation. Otherwise, it's likely* to be a linear operation for operator++.

*citation needed

Since next is already doing all of the necessary work, it makes sense to me for it to also return the distance taken. We also get this information from the bounded advance.

Proposed resolution

[range.iter.op.next]

template<input_or_output_iterator I, sentinel_for<I> S>
  constexpr next_result<I,  iter_difference_t<I>>
    ranges::next(I x, S bound);

Effects: Equivalent to:

auto steps = ranges::advance(x, bound);
return {std::move(x), steps};
template<input_or_output_iterator I, sentinel_for<I> S>
  constexpr next_result<I,  iter_difference_t<I>>
    ranges::next(I x, iter_difference_t<I> n, S bound);

Effects: Equivalent to:

auto steps = ranges::advance(x, n, bound);
return {std::move(x), steps};

[range.iter.op.prev]

template<bidirectional_iterator I, sentinel_for<I> S>
  constexpr prev_result<I,  iter_difference_t<I>>
    ranges::prev(I x, iter_difference_t<I> n, S bound);

Effects: Equivalent to:

auto steps = ranges::advance(x, -n, bound);
return {std::move(x), steps};
@cjdb cjdb changed the title ranges::next, prev, and advance should also return the advanced distance Bounded next and prev should also return the distance advanced Aug 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant