Title
std::prev should not require BidirectionalIterator
Status
new
Section
[iterator.operations]
Submitter
Billy O'Neal III

Created on 2019-04-03.00:00:00 last changed 6 months ago

Messages

Date: 2024-06-18.11:40:55

Proposed resolution:

This wording is relative to N4810.

[Drafting Note: Three mutually exclusive options are prepared, depicted below by Option A, Option B, and Option C, respectively.]

Option A

  1. NAD, the name BidirectionalIterator actually means that prev requires bidirectional iterators, in which case this change to libcxx is incorrect.

Option B

  1. Modify [iterator.synopsis], header <iterator> synopsis, as indicated:

    // [iterator.operations], iterator operations
    […]
    template<class BidirectionalInputIterator>
      constexpr BidirectionalInputIterator prev(BidirectionalInputIterator x,
        typename iterator_traits<BidirectionalInputIterator>::difference_type n = 1);
    
  2. Modify [iterator.operations] as indicated:

    template<class BidirectionalInputIterator>
      constexpr BidirectionalInputIterator prev(BidirectionalInputIterator x,
        typename iterator_traits<BidirectionalInputIterator>::difference_type n = 1);
    

    -7- Effects: Equivalent to: advance(x, -n); return x;

Option C

  1. The intent of the wording is that the template parameters apply requirements, and the defect is that they do not. We should add a requirement in [iterator.operations]/1 to the effect that the template parameter names impose said requirements.

Date: 2024-06-15.00:00:00

[ 2024-06-18; Jonathan adds a comment ]

Related to LWG 2353 which made a similar change to `std::next`. Also, if we require a Cpp17BidirectionalIterator here, then that means you can't use `std::prev` on a `std::bidirectional_iterator` unless it also meets the Cpp17BidirectionalIterator requirements. That seems like an unnecessary restriction, since `std::prev` doesn't do anything that wouldn't work fine with any type that models `std::bidirectional_iterator`.

Date: 2022-04-15.00:00:00

[ 2022-04-22; Jonathan adds a comment ]

P2408 changes the requirements for types substituting BidirectionalIterator etc. in the Algorithms clause. We should consider whether that is appropriate here, especially as algorithms might make use of std::prev internally. An algorithm that was changed by P2408 to accept types that model bidirectional_iterator instead of requiring Cpp17BidirectionalIterator might have to stop using std::prev if we don't resolve this issue to allow it.

We should consider whether distance, advance and next need the same treatment.

Date: 2019-06-12.00:00:00

[ 2019-06-12 Priority set to 3 after reflector discussion ]

Date: 2019-04-03.00:00:00

MSVC++ (and apparently libc++) have asserts that std::prev only accepts BidirectionalIterators, because it's declared in the standard as accepting only BidirectionalIterator. libc++ changed their tests (in this commit), apparently from a bug report from Ville and Jonathan, saying that one could theoretically call std::prev with a negative number.

The standardese in [iterator.operations] strongly indicates that prev requires a BidirectionalIterator, but I don't see the usual wording that connects template type parameters of that name to the <algorithm> requirements or similar. So perhaps one could argue that the name Bidirectional there has no meaning. Even if that is the case, that's a defect in the other direction.

History
Date User Action Args
2024-06-18 11:40:55adminsetmessages: + msg14166
2022-04-22 17:05:58adminsetmessages: + msg12420
2019-06-10 05:16:56adminsetmessages: + msg10431
2019-04-06 16:02:43adminsetmessages: + msg10385
2019-04-03 00:00:00admincreate