C++ · Module 10 · Lesson 21 min read

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:

ResultMeaning
< 0a is less than b
== 0equal (equivalent)
> 0a is greater than b

Defaulted comparisons ✅

arduino
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

vbnet
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

TypeMeaningExample
std::strong_orderingequal means identicalints, strings
std::weak_orderingequivalent but distinguishablecase-insensitive strings
std::partial_orderingsome values incomparabledoubles (NaN)

Why it matters

Sorting (std::sort), std::set, std::map keys and searching all need <. Defaulted <=> gives correct, consistent comparisons for free.

Example

C++
#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
*/