Comparisons & the Spaceship Operator <=>
Before C++20
To make a type fully comparable you had to write ==, !=, <, <=, >, >= — six functions, easy to get inconsistent.
The three-way comparison <=> (C++20, <compare>)
a <=> b returns an ordering:
| Result | Meaning |
|---|---|
< 0 | a is less than b |
== 0 | equal (equivalent) |
> 0 | a is greater than b |
Defaulted comparisons ✅
struct Version {
int major, minor, patch;
auto operator<=>(const Version&) const = default; // also gives you ==
};
The compiler compares members in declaration order (lexicographically) and generates <, <=, >, >= from <=>, and ==/!= from the defaulted ==.
Rewritten expressions
With <=> defined, the compiler rewrites a < b as (a <=> b) < 0. a != b is rewritten as !(a == b). You only write <=> and (if needed) ==.
Custom ordering
std::strong_ordering operator<=>(const Expense& o) const {
if (auto c = date <=> o.date; c != 0) return c; // first by date
return o.amount <=> amount; // then by amount, descending
}
bool operator==(const Expense& o) const = default;
⚠️ If you write a custom <=>, also define == (a custom <=> does not generate ==).
Ordering categories
| Type | Meaning | Example |
|---|---|---|
std::strong_ordering | equal means identical | ints, strings |
std::weak_ordering | equivalent but distinguishable | case-insensitive strings |
std::partial_ordering | some values incomparable | doubles (NaN) |
Why it matters
Sorting (std::sort), std::set, std::map keys and searching all need <. Defaulted <=> gives correct, consistent comparisons for free.
Example
#include <algorithm>
#include <compare>
#include <iostream>
#include <string>
#include <vector>
struct Version {
int major, minor, patch;
auto operator<=>(const Version&) const = default; // all six comparisons
};
std::ostream& operator<<(std::ostream& os, const Version& v) {
return os << v.major << '.' << v.minor << '.' << v.patch;
}
struct Expense {
std::string date; // "YYYY-MM-DD" sorts correctly as text
double amount;
std::string note;
std::partial_ordering operator<=>(const Expense& o) const {
if (auto c = date <=> o.date; c != 0) return c; // by date first
return o.amount <=> amount; // then larger amounts first
}
bool operator==(const Expense& o) const { return date == o.date && amount == o.amount; }
};
int main() {
Version installed{ 2, 9, 14 }, latest{ 2, 10, 0 };
std::cout << std::boolalpha;
std::cout << installed << " < " << latest << "? " << (installed < latest) << '\n';
std::cout << installed << " == " << Version{ 2, 9, 14 } << "? " << (installed == Version{ 2, 9, 14 }) << '\n';
std::cout << "update needed? " << (installed != latest) << '\n';
auto cmp = 3 <=> 5;
std::cout << "3 <=> 5 is less? " << (cmp < 0) << '\n';
std::vector<Version> versions = { { 1, 2, 0 }, { 1, 10, 0 }, { 1, 2, 3 }, { 0, 9, 9 } };
std::sort(versions.begin(), versions.end()); // uses the generated <
std::cout << "sorted versions:";
for (const auto& v : versions) std::cout << ' ' << v;
std::cout << '\n';
std::vector<Expense> expenses = {
{ "2026-11-03", 450, "lunch" }, { "2026-11-01", 15000, "rent" },
{ "2026-11-03", 1200, "groceries" }, { "2026-11-01", 300, "bus" } };
std::sort(expenses.begin(), expenses.end());
for (const auto& e : expenses) std::cout << e.date << " " << e.amount << " " << e.note << '\n';
std::cout << "max element: " << std::max_element(expenses.begin(), expenses.end())->note << '\n';
return 0;
}
/* Output:
2.9.14 < 2.10.0? true
2.9.14 == 2.9.14? true
update needed? true
3 <=> 5 is less? true
sorted versions: 0.9.9 1.2.0 1.2.3 1.10.0
2026-11-01 15000 rent
2026-11-01 300 bus
2026-11-03 1200 groceries
2026-11-03 450 lunch
max element: lunch
*/