[expr.spaceship] (original) (raw)

7 Expressions [expr]

7.6 Compound expressions [expr.compound]

7.6.8 Three-way comparison operator [expr.spaceship]

The expression p <=> q is a prvalue indicating whetherp is less than, equal to, greater than, or incomparable withq.

If one of the operands is of type booland the other is not, the program is ill-formed.

If both operands have arithmetic types, or one operand has integral type and the other operand has unscoped enumeration type, the usual arithmetic conversions are applied to the operands.

Then:

If both operands have the same enumeration type E, the operator yields the result of converting the operands to the underlying type of Eand applying <=> to the converted operands.

If at least one of the operands is of pointer type and the other operand is of pointer or array type, array-to-pointer conversions ([conv.array]), pointer conversions ([conv.ptr]), andqualification conversionsare performed on both operands to bring them to their composite pointer type ([expr.type]).

After the conversions, the operands shall have the same type.

If the composite pointer type is an object pointer type,p <=> q is of type std​::​strong_­ordering.

If two pointer operands p and q compare equal ([expr.eq]),p <=> q yields std​::​strong_­ordering​::​equal; if p and q compare unequal,p <=> q yieldsstd​::​strong_­ordering​::​lessif q compares greater than pandstd​::​strong_­ordering​::​greaterif p compares greater than q ([expr.rel]).

Otherwise, the result is unspecified.

Otherwise, the program is ill-formed.

The three comparison category types ([cmp.categories]) (the typesstd​::​strong_­ordering,std​::​weak_­ordering, andstd​::​partial_­ordering) are not predefined; if the header <compare> ([compare.syn]) is not imported or included prior to a use of such a class type – even an implicit use in which the type is not named (e.g., via the auto specifierin a defaulted three-way comparisonor use of the built-in operator) – the program is ill-formed.