Title
lexicographical_compare_three_way is overspecified
Status
c++23
Section
[alg.three.way]
Submitter
Casey Carter

Created on 2020-02-27.00:00:00 last changed 12 months ago

Messages

Date: 2021-06-07.16:58:04

Proposed resolution:

This wording is relative to N4885.

  1. Modify [alg.three.way] as indicated:

    template<class InputIterator1, class InputIterator2, class Cmp>
      constexpr auto
        lexicographical_compare_three_way(InputIterator1 b1, InputIterator1 e1,
                                          InputIterator2 b2, InputIterator2 e2,
                                          Cmp comp)
            -> decltype(comp(*b1, *b2));
    

    -?- Let N be min(e1 - b1, e2 - b2). Let E(n) be comp(*(b1 + n), *(b2 + n)).

    -1- Mandates: decltype(comp(*b1, *b2)) is a comparison category type.

    -?- Returns: E(i), where i is the smallest integer in [0, N) such that E(i) != 0 is true, or (e1 - b1) <=> (e2 - b2) if no such integer exists.

    -?- Complexity: At most N applications of comp.

    -2- Effects: Lexicographically compares two ranges and produces a result of the strongest applicable comparison category type. Equivalent to:

    for ( ; b1 != e1 && b2 != e2; void(++b1), void(++b2) )
      if (auto cmp = comp(*b1,*b2); cmp != 0)
          return cmp;
    return b1 != e1 ? strong_ordering::greater :
           b2 != e2 ? strong_ordering::less :
                      strong_ordering::equal;
    
Date: 2021-06-07.00:00:00

[ 2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting → WP. ]

Date: 2021-05-15.00:00:00

[ 2021-05-26; Reflector poll ]

Set status to Tentatively Ready after five votes in favour during reflector poll.

Date: 2021-05-19.00:00:00

[ 2021-05-19 Tim adds wording ]

The wording below simply respecifies the algorithm in words. It seems pointless to try to optimize the code when the reading in the discussion above would entirely rule out things like memcmp optimizations for arbitrary contiguous iterators of bytes.

Date: 2020-03-29.00:00:00

[ 2020-03-29 Issue Prioritization ]

Priority to 3 after reflector discussion.

Date: 2020-02-27.00:00:00

[alg.three.way]/2 specifies the effects of the lexicographical_compare_three_way algorithm via a large "equivalent to" codeblock. This codeblock specifies one more iterator comparison than necessary when the first input sequence is greater than the second, and two more than necessary in other cases. Requiring unnecessary work is the antithesis of C++.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2021-06-07 16:58:04adminsetmessages: + msg11884
2021-06-07 16:58:04adminsetstatus: voting -> wp
2021-05-26 21:11:22adminsetstatus: ready -> voting
2021-05-26 10:53:33adminsetmessages: + msg11855
2021-05-26 10:53:33adminsetstatus: new -> ready
2021-05-20 01:59:30adminsetmessages: + msg11825
2021-05-20 01:59:30adminsetmessages: + msg11824
2020-02-29 16:31:18adminsetmessages: + msg11154
2020-02-27 00:00:00admincreate