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 valuesstd::weak_ordering— equivalent but distinguishable (e.g., case-insensitive)std::partial_ordering— some values are incomparable (e.g., NaN)
All lessons in this course
- Arithmetic and Comparison Operators
- Stream Insertion and Extraction Operators
- Subscript and Function Call Operators
- Three-Way Comparison Operator C++20