Concept Subspace :: sus :: cmp :: Ord

template <class Lhs, class Rhs = Lhs>
concept Ord
requires
StrongOrd<Lhs, Rhs> || requires(const std::remove_reference_t<Lhs>& lhs,
                                    const std::remove_reference_t<Rhs>& rhs) {
      { lhs <=> rhs } -> std::same_as<std::weak_ordering>;
    }

Concept for types that form a total ordering (aka std::weak_ordering).

Types that satisfy Ord can be sorted or compared and always return a consistent result as all possible values are comparable. Objects that compare as equivalent for ordering may still be different internally and compare as different through operator==. If unique identity is required, use StrongOrd. Otherwise, typically use Ord for constraining types that will be ordered.

How can I implement Ord?

Ord requires that the type has operator<=> which returns std::weak_ordering (or std::strong_ordering). It will implicitly also be Ord and PartialOrd as a result.

Lexicographical comparison

Lexicographical comparison is an operation with the following properties:

  • Two sequences are compared element by element.
  • The first mismatching element defines which sequence is lexicographically less or greater than the other.
  • If one sequence is a prefix of another, the shorter sequence is lexicographically less than the other.
  • If two sequence have equivalent elements and are of the same length, then the sequences are lexicographically equal.
  • An empty sequence is lexicographically less than any non-empty sequence.
  • Two empty sequences are lexicographically equal.

Ord and Eq interations

While Ord can report equality, it does not imply that the type satisfies Eq, and a separate operator== is required for that concept. Unlike StrongOrd, it is not required that objects which are ordered as equivalent also compare as equal with operator==.

Generic code that requires a type to be Ord should take care to use operator<=> and not operator== unless also requiring Eq, in which case consider requiring StrongOrd in place of both Ord and Eq.

Determining Ord strictly

Ord will be also satisfied if the types satisfy StrongOrd. To determine if a Ord is the strongest type of ordering between the types, use ExclusiveOrd.