[class.spaceship] # 11 Classes [[class]](./#class) ## 11.10 Comparisons [[class.compare]](class.compare#class.spaceship) ### 11.10.3 Three-way comparison [class.spaceship] [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6731) The [*synthesized three-way comparison*](#def:three-way_comparison,synthesized "11.10.3 Three-way comparison [class.spaceship]") of type R ([[cmp.categories]](cmp.categories "17.12.2 Comparison category types")) of glvalues a and b of the same type is defined as follows: - [(1.1)](#1.1) If a <=> b is usable ([[class.compare.default]](class.compare.default "11.10.1 Defaulted comparison operator functions")) and can be explicitly converted to R using static_cast,static_cast(a <=> b)[.](#1.1.sentence-1) - [(1.2)](#1.2) 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[.](#1.2.sentence-1) - [(1.3)](#1.3) Otherwise, if R is not a comparison category type, or either the expression a == b or the expression a < b is not usable, the synthesized three-way comparison is not defined[.](#1.3.sentence-1) - [(1.4)](#1.4) Otherwise, if R is strong_ordering, thena == b ? strong_ordering::equal : a < b ? strong_ordering::less : strong_ordering::greater - [(1.5)](#1.5) Otherwise, if R is weak_ordering, thena == b ? weak_ordering::equivalent : a < b ? weak_ordering::less : weak_ordering::greater - [(1.6)](#1.6) 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](#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[.](#1.sentence-2) — *end note*] [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6786) Let R be the declared return type of a defaulted three-way comparison operator function, and let xi be the elements of the expanded list of subobjects for an object x of type C[.](#2.sentence-1) - [(2.1)](#2.1) If R is auto, then let cvi Ri be the type of the expression xi <=> xi[.](#2.1.sentence-1) The operator function is defined as deleted if that expression is not usable or if Ri is not a comparison category type ([[cmp.categories.pre]](cmp.categories.pre "17.12.2.1 Preamble")) for any i[.](#2.1.sentence-2) The return type is deduced as the common comparison type (see below) ofR0, R1, …, Rn−1[.](#2.1.sentence-3) - [(2.2)](#2.2) Otherwise, R shall not contain a placeholder type[.](#2.2.sentence-1) If the synthesized three-way comparison of type R between any objects xi and xi is not defined, the operator function is defined as deleted[.](#2.2.sentence-2) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6813) The return value of type R of the defaulted three-way comparison operator function with parameters x and y of the same type is determined by comparing corresponding elementsxi and yi 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 R between xi and yi yields a result value vi where vi!=0, contextually converted to bool, yields true[.](#3.sentence-1) The return value is a copy of vi if such an index exists andstatic_cast(std​::​strong_ordering​::​equal) otherwise[.](#3.sentence-2) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6830) The [*common comparison type*](#def:common_comparison_type "11.10.3 Three-way comparison [class.spaceship]") U of a possibly-empty list of n comparison category typesT0, T1, …, Tn−1 is defined as follows: - [(4.1)](#4.1) If at least one Ti is std​::​partial_ordering,U is std​::​partial_ordering ([[cmp.partialord]](cmp.partialord "17.12.2.2 Class partial_­ordering"))[.](#4.1.sentence-1) - [(4.2)](#4.2) Otherwise, if at least one Ti is std​::​weak_ordering,U is std​::​weak_ordering ([[cmp.weakord]](cmp.weakord "17.12.2.3 Class weak_­ordering"))[.](#4.2.sentence-1) - [(4.3)](#4.3) Otherwise, U is std​::​strong_ordering ([[cmp.strongord]](cmp.strongord "17.12.2.4 Class strong_­ordering"))[.](#4.3.sentence-1) [*Note [2](#note-2)*: In particular, this is the result when n is 0[.](#4.3.sentence-2) — *end note*]