Title
Incorrect requires for move_backward and copy_backward
Status
c++11
Section
[alg.move]
Submitter
Howard Hinnant

Created on 2009-09-13.00:00:00 last changed 162 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

Change [alg.move], p6:

template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2
    move_backward(BidirectionalIterator1 first,
                  BidirectionalIterator1 last,
                  BidirectionalIterator2 result);

...

Requires: result shall not be in the range [(first,last]).

Change [alg.copy], p13:

template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2
    copy_backward(BidirectionalIterator1 first,
                  BidirectionalIterator1 last,
                  BidirectionalIterator2 result);

...

Requires: result shall not be in the range [(first,last]).

Date: 2010-10-21.18:28:33

[ 2010 Pittsburgh: Moved to Ready. ]

Date: 2009-09-13.00:00:00

[alg.move], p6 says:

template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2
    move_backward(BidirectionalIterator1 first,
                  BidirectionalIterator1 last,
                  BidirectionalIterator2 result);

...

Requires: result shall not be in the range [first,last).

This is essentially an "off-by-one" error.

When result == last, which is allowed by this specification, then the range [first, last) is being move assigned into the range [first, last). The move (forward) algorithm doesn't allow self move assignment, and neither should move_backward. So last should be included in the range which result can not be in.

Conversely, when result == first, which is not allowed by this specification, then the range [first, last) is being move assigned into the range [first - (last-first), first). I.e. into a non-overlapping range. Therefore first should not be included in the range which result can not be in.

The same argument applies to copy_backward though copy assigning elements to themselves (result == last) should be harmless (though is disallowed by copy).

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2010-10-21 19:00:35adminsetstatus: ready -> wp
2010-10-21 18:28:33adminsetmessages: + msg1147
2010-10-21 18:28:33adminsetmessages: + msg1146
2009-09-13 00:00:00admincreate