[class.spaceship] (original) (raw)
11 Classes [class]
11.10 Comparisons [class.compare]
11.10.3 Three-way comparison [class.spaceship]
The synthesized three-way comparisonof type R ([cmp.categories]) of glvalues a and b of the same type is defined as follows:
- If a <=> b is usable ([class.compare.default]) and can be explicitly converted to R using static_cast,static_cast<R>(a <=> b).
- Otherwise, if a <=> b is usable or overload resolution for a <=> b is performed and finds at least one viable candidate, the synthesized three-way comparison is not defined.
- Otherwise, if R is not a comparison category type, or either the expression a == b or the expression a < bis not usable, the synthesized three-way comparison is not defined.
- Otherwise, if R is strong_ordering, thena == b ? strong_ordering::equal :a < b ? strong_ordering::less : strong_ordering::greater
- Otherwise, if R is weak_ordering, thena == b ? weak_ordering::equivalent :a < b ? weak_ordering::less : weak_ordering::greater
- Otherwise (when R is partial_ordering),a == b ? partial_ordering::equivalent :a < b ? partial_ordering::less :b < a ? partial_ordering::greater : partial_ordering::unordered
[Note 1:
A synthesized three-way comparison is ill-formed if overload resolution finds usable candidates that do not otherwise meet the requirements implied by the defined expression.
— _end note_]
Let R be the declared return type of a defaulted three-way comparison operator function, and let be the elements of the expanded list of subobjects for an object x of type C.
- If R is auto, then let be the type of the expression .
The operator function is defined as deleted if that expression is not usable or if is not a comparison category type ([cmp.categories.pre]) for any i.
The return type is deduced as the common comparison type (see below) of, , …, . - Otherwise, R shall not contain a placeholder type.
If the synthesized three-way comparison of type Rbetween any objects and is not defined, the operator function is defined as deleted.
The return value of type Rof the defaulted three-way comparison operator function with parameters x and y of the same type is determined by comparing corresponding elements and in the expanded lists of subobjects for x and y(in increasing index order) until the first index i where the synthesized three-way comparison of type Rbetween and yields a result value where , contextually converted to bool, yields true.
The return value is a copy of if such an index exists andstatic_cast<R>(std::strong_ordering::equal) otherwise.
The common comparison type Uof a possibly-empty list of n comparison category types, , …, is defined as follows:
- If at least one is std::partial_ordering,U is std::partial_ordering ([cmp.partialord]).
- Otherwise, if at least one is std::weak_ordering,U is std::weak_ordering ([cmp.weakord]).
- [Note 2:
In particular, this is the result when n is 0.
— _end note_]