Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::partial_ordering — a partial comparison result

std::partial_ordering from <compare> represents a comparison result that can be less, equivalent, greater, or unordered. The unordered state means the values are incomparable, not equivalent.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <compare>
std::partial_ordering r = std::partial_ordering::unordered;
r < 0;
r == 0;
r == std::partial_ordering::unordered;

std::partial_ordering from <compare> is a result category for the three-way comparison operator<=>. It permits incomparable values; equivalence need not guarantee substitutability.

The four named results are less, equivalent, greater, and unordered. For a result r, the expressions r < 0, r == 0, and r > 0 recognize the first three states. For unordered, all three are false, as are r <= 0 and r >= 0. Detect that state with r == std::partial_ordering::unordered; r != 0 alone does not distinguish it from less and greater.

When comparing a result against zero, use the literal 0, not a variable or another number. The standard specifically requires the literal; supplying another argument to that parameter causes undefined behavior. Comparing two category objects with == is a separate, valid operation.

The example uses named constants to demonstrate incomparability deterministically, without depending on floating-point representation.

Examples

#include <compare>
#include <iostream>

int main() {
    constexpr auto less = std::partial_ordering::less;
    constexpr auto unordered = std::partial_ordering::unordered;

    static_assert(less < 0);
    static_assert(unordered != 0);
    static_assert(unordered == std::partial_ordering::unordered);

    std::cout << std::boolalpha;
    std::cout << (less < 0) << '\n';
    std::cout << (unordered < 0) << ' '
              << (unordered == 0) << ' '
              << (unordered > 0) << '\n';
    std::cout << (unordered == std::partial_ordering::unordered) << '\n';
}

Result

true
false false false
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources