TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
<=> operator, formally known as the three-way comparison operator (and colloquially as the “spaceship operator”), is a C++20 language feature that evaluates the relative order of two operands in a single expression. Instead of returning a boolean, it returns a comparison category object indicating whether the left operand is less than, equal to/equivalent to, or greater than the right operand.
Comparison Categories
The operator relies on types defined in the<compare> header. It does not return an integer; it returns one of three standard comparison category types, which dictate the mathematical properties of the comparison:
std::strong_ordering: Represents a strict total ordering. Ifa <=> b == std::strong_ordering::equal, thenaandbare indistinguishable and perfectly substitutable.- Valid values:
less,equal,greater. - Typical for integral types (
int,char, pointers).
- Valid values:
std::weak_ordering: Represents a weak total ordering. Ifa <=> b == std::weak_ordering::equivalent,aandbevaluate as equivalent for sorting purposes, but their underlying states may differ (e.g., case-insensitive string comparison).- Valid values:
less,equivalent,greater.
- Valid values:
std::partial_ordering: Represents a partial ordering where some values may be incomparable.- Valid values:
less,equivalent,greater,unordered. - Typical for floating-point types where
NaN(Not a Number) cannot be ordered relative to other values.
- Valid values:
Evaluation Mechanics
The result of the<=> operator is designed to be compared against literal 0. The compiler translates traditional relational operators into <=> expressions when the traditional operators are not explicitly defined. The spaceship operator is strictly used to rewrite the four relational operators:
a < bevaluates as(a <=> b) < 0a > bevaluates as(a <=> b) > 0a <= bevaluates as(a <=> b) <= 0a >= bevaluates as(a <=> b) >= 0
==) and inequality (!=) are never rewritten to use operator<=>. They are resolved strictly using operator== (with operand reversal if necessary).
Defaulting the Operator
When explicitly defaulted inside a class definition, the compiler automatically generates the<=> operator using a lexicographical, member-wise comparison. It compares base classes (left-to-right) and non-static data members in their exact order of declaration.
operator<=> implicitly declares and defaults operator== as well. The generated operator== performs direct member-wise equality checks; it does not evaluate (a <=> b) == 0. Consequently, a single = default declaration provides the class with all six relational operators (==, !=, <, <=, >, >=).
Custom Implementation
When writing a custom<=> operator, you must explicitly specify the return type or use auto to let the compiler deduce it based on the returned comparison category.
Operator Rewriting and Symmetry
The compiler performs expression rewriting to ensure symmetry. If an expressiona < b is encountered and the left operand a does not have a matching < or <=> operator, the compiler will attempt to rewrite the expression as 0 < (b <=> a), reversing the operands and utilizing the right operand’s operator. This eliminates the need to write multiple overloads for heterogeneous comparisons (e.g., comparing a class with an int in both obj < 5 and 5 < obj orders). The same operand reversal mechanism applies to operator== for equality checks.
Master C++ with Deep Grasping Methodology!Learn More





