CKTIS / C++
std::strong_ordering — a strong ordering result
std::strong_ordering from <compare> describes a three-way comparison result: less, equal, or greater. It represents a total order with substitutability of equal values and has no unordered state.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <compare>
std::strong_ordering result = a <=> b;
result < 0;
result == 0;
result > 0;std::strong_ordering is a comparison category type from <compare>, not a number or a bool. The result of a <=> b for operands of type int has this type. Include the header before using the operator.
The constants less, equal, and greater represent three outcomes; equivalent has the same value as equal. A result can be implicitly converted to std::weak_ordering or std::partial_ordering.
Strong ordering assumes substitutability: if a == b, then f(a) == f(b) for functions reading only comparison-salient state accessible through public const members. Merely choosing the result type does not prove this property. There is no unordered state; built-in <=> for floating-point numbers returns std::partial_ordering instead.
Test the direction with result < 0, result == 0, or result > 0. Use literal 0 in these overloads, not a zero-valued variable or another number: an argument other than literal 0 causes undefined behavior. You can also test result == std::strong_ordering::less.
The example compares 7 with 12, so the expected output is true, false, false, in that order. The static_assert checks the result during compilation. The displayed source was run unchanged in an isolated environment, and its output matched the expected output exactly. Neither that run nor the -std=c++23 flag establishes compatibility with the production runtime environment.
Examples
#include <compare>
#include <iostream>
int main() {
constexpr std::strong_ordering result = 7 <=> 12;
static_assert(result == std::strong_ordering::less);
static_assert(std::strong_ordering::equal ==
std::strong_ordering::equivalent);
std::cout << std::boolalpha
<< (result < 0) << '\n'
<< (result == 0) << '\n'
<< (result > 0) << '\n';
}
Result
true false false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.