Title
nth_element requires inconsistent post-conditions
Status
c++14
Section
[alg.nth.element]
Submitter
Peter Sommerlad

Created on 2012-07-06.00:00:00 last changed 131 months ago

Messages

Date: 2013-04-19.22:22:17

Proposed resolution:

This wording is relative to N3376.

  1. Change [alg.nth.element] p1 as indicated:

    template<class RandomAccessIterator>
    void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last);
    template<class RandomAccessIterator, class Compare>
    void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last, Compare comp);
    

    -1- After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first,nth) and any iterator j in the range [nth,last) it holds that: !(*i > *j)!(*j < *i) or comp(*j, *i) == false.

Date: 2013-04-20.00:00:00

[ 2013-04-20 Bristol ]

Date: 2012-10-16.15:35:12

[ 2012-10 Portland: Move to Ready ]

This is clearly correct by inspection, moved to Ready by unanimous consent.

Date: 2012-07-06.00:00:00

The specification of nth_element refers to operator< whereas all sorting without a compare function is based on operator<. While it is true that for all regular types both operators should be defined accordingly, all other sorting algorithms only rely on existence of operator<. So I guess the paragraph p1

After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first,nth) and any iterator j in the range [nth,last) it holds that: !(*i > *j) or comp(*j, *i) == false.

should read

After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first,nth) and any iterator j in the range [nth,last) it holds that: !(*j < *i) or comp(*j, *i) == false.

Note only "!(*i > *j)" was changed to "!(*j < *i)" and it would be more symmetric with comp(*j, *i) as well.

In theory this might be a semantic change to the spec, but I believe the mistake is unintended.

History
Date User Action Args
2014-02-20 13:20:35adminsetstatus: wp -> c++14
2013-04-25 19:07:07adminsetstatus: voting -> wp
2013-04-19 22:22:17adminsetmessages: + msg6475
2013-04-19 22:22:17adminsetstatus: ready -> voting
2012-10-16 15:35:12adminsetmessages: + msg6175
2012-10-16 15:35:12adminsetstatus: new -> ready
2012-08-05 17:14:08adminsetmessages: + msg6098
2012-07-06 00:00:00admincreate