5

Why is there std::lexicographical_compare_three_way, but no std::ranges::lexicographical_compare_three_way?

There is argument Comp in std::ranges::lexicographical_compare, but it is rather useless, because function returns bool, when one of the comparison category types is expected.

Here are some links to cppref
https://en.cppreference.com/w/cpp/algorithm/lexicographical_compare_three_way
https://en.cppreference.com/w/cpp/algorithm/ranges/lexicographical_compare

Barry
  • 286,269
  • 29
  • 621
  • 977
SherAndrei
  • 528
  • 3
  • 14

1 Answers1

6

Why is there std::lexicographical_compare_three_way, but no std::ranges::lexicographical_compare_three_way?

The algorithms in std::ranges are constrained by concepts while the algorithms in std are not. So, for instance, std::ranges::lexicographical_compare (which uses a two-way comparison, defaulting to <) is specified as:

template<input_­range R1, input_­range R2, class Proj1 = identity,
         class Proj2 = identity,
         indirect_­strict_­weak_­order<projected<iterator_t<R1>, Proj1>,
                                    projected<iterator_t<R2>, Proj2>> Comp = ranges::less>
  constexpr bool
    lexicographical_compare(R1&& r1, R2&& r2, Comp comp = {},
                            Proj1 proj1 = {}, Proj2 proj2 = {});

Notably, we have the concept indirect_strict_weak_order to explain the syntactic and semantic requirements for the comparison operation you need in order for this algorithm to be meaningful.

However, nobody has (yet) done the work to formulate the corresponding concepts for three-way comparison. Note that strict_weak_order has very strong semantic requirements, and we would need the equivalent for a hypothetical strict_weak_order_three_way (or some other name that sucks less).

The interesting thing here is that we need to not reject partial_ordering, but rather only require that no two elements in the domain compare as partial_ordering::unordered.

Once we have such concepts, then specifying and implementing std::ranges::lexicographical_compare_three_way is pretty straightforward.

Barry
  • 286,269
  • 29
  • 621
  • 977