0Pricing
C++ Academy · Lesson

Three-Way Comparison Operator C++20

Use the spaceship operator for concise, consistent comparisons.

Three-Way Comparison Operator C++20 is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Spaceship Operator <=>

C++20 introduced the three-way comparison operator <=>. One operator gives the compiler everything it needs to generate all six relational operators.

Return Types: Three Categories

The return type depends on the ordering strength:

  • std::strong_ordering — distinguishable equivalent values
  • std::weak_ordering — equivalent but distinguishable (e.g., case-insensitive)
  • std::partial_ordering — some values are incomparable (e.g., NaN)

Defaulted Comparison

The most common pattern: ask the compiler to generate <=> by comparing members lexicographically.

struct Point {
    int x;
    int y;
    auto operator<=>(const Point& other) const = default;
};

Point a{1, 2}, b{1, 3};
bool less = (a < b);     // true — auto-generated

All Six for Free

Defaulting <=> also defaults ==. Together they generate !=, <, <=, >, >= — all six in one line.

Custom <=> Implementation

Write the body manually for custom comparison logic.

std::strong_ordering operator<=>(const Person& other) const {
    if (auto c = name <=> other.name; c != 0) return c;
    return age <=> other.age;
}

Using the Result

The result of <=> can be compared with literal 0 for the boolean check you actually want.

if ((a <=> b) < 0)  std::cout << "a less\n";
if ((a <=> b) == 0) std::cout << "equal\n";
if ((a <=> b) > 0)  std::cout << "a greater\n";

Floating Point and partial_ordering

For floats, use std::partial_ordering to handle NaN — comparing with NaN yields unordered.

struct Measurement {
    double value;
    std::partial_ordering operator<=>(const Measurement&) const = default;
};

Weak Ordering Example

Use weak ordering when values may be distinguishable but compare equal — case-insensitive strings, for example.

Custom == Independently

You can default <=> and write a custom == — sometimes equality has different rules from ordering.

Migrating Old Code

If your class previously had a bunch of comparison operators, replace them with a defaulted <=> when migrating to C++20. Much less code, same semantics.

When Not to Use <=>

For types where only equality matters (no order), provide only operator== and operator!= (which is auto-synthesized in C++20).

Performance Note

The compiler may optimize defaulted <=> as efficiently as hand-written comparisons. No runtime overhead for using the spaceship.

Quick Check

Which return category should a comparison use for floating-point types that may contain NaN?

Recap

The C++20 <=> operator generates all six relational operators from a single definition. Default it to compare members lexicographically. Choose strong, weak, or partial ordering based on your type s semantics.

Frequently asked questions

Is the “Three-Way Comparison Operator C++20” lesson free?

Yes — the full text of “Three-Way Comparison Operator C++20” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Three-Way Comparison Operator C++20”?

Use the spaceship operator for concise, consistent comparisons. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Three-Way Comparison Operator C++20” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Comparison Operators
  2. Stream Insertion and Extraction Operators
  3. Subscript and Function Call Operators
  4. Three-Way Comparison Operator C++20
← Back to C++ Academy