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 2019-06-10.05:16:56.

Messages

Date: 2019-06-10.05:16:56

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: 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
2019-06-10 05:16:56adminsetmessages: + msg10431
2019-04-06 16:02:43adminsetmessages: + msg10385
2019-04-03 00:00:00admincreate