0Pricing
C++ Academy · Lesson

Three-Way Comparison Operator C++20

Use the spaceship operator for concise, consistent comparisons.

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)

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