Difference between revisions of "cpp/utility/compare/partial ordering"
m (templated substituability) |
m (~) |
||
(13 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|partial_ordering}} | {{cpp/title|partial_ordering}} | ||
{{cpp/utility/navbar}} | {{cpp/utility/navbar}} | ||
− | {{ddcl | since=c++20 | header=compare| 1= | + | {{ddcl|since=c++20|header=compare|1= |
class partial_ordering; | class partial_ordering; | ||
}} | }} | ||
− | The class type {{tt|std::partial_ordering}} is the result type of a [[cpp/language/ | + | The class type {{tt|std::partial_ordering}} is the result type of a [[cpp/language/operator comparison#Three-way comparison|three-way comparison]] that: |
− | * | + | * Admits all six relational operators ({{tt|1===}}, {{tt|1=!=}}, {{tt|<}}, {{tt|1=<=}}, {{tt|>}}, {{tt|1=>=}}). |
{{cpp/utility/compare/substitutable|no}} | {{cpp/utility/compare/substitutable|no}} | ||
− | * | + | * {{enwiki|Connected relation|Admits incomparable values}}: {{c|a < b}}, {{c|1=a == b}}, and {{c|a > b}} may all be {{c|false}}. |
===Constants=== | ===Constants=== | ||
The type {{tt|std::partial_ordering}} has four valid values, implemented as const static data members of its type: | The type {{tt|std::partial_ordering}} has four valid values, implemented as const static data members of its type: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem | | + | {{dsc hitem|Name|Definition}} |
− | {{dsc mem sconst | | + | {{dsc mem sconst|{{dsc small|{{c/core|inline constexpr std::partial_ordering}}}} less|nolink=true|a valid value indicating less-than (ordered before) relationship}} |
− | {{dsc mem sconst | | + | {{dsc mem sconst|{{dsc small|{{c/core|inline constexpr std::partial_ordering}}}} equivalent|nolink=true|a valid value indicating equivalence (neither ordered before nor ordered after)}} |
− | {{dsc mem sconst | | + | {{dsc mem sconst|{{dsc small|{{c/core|inline constexpr std::partial_ordering}}}} greater|nolink=true|a valid value indicating greater-than (ordered after) relationship}} |
− | {{dsc mem sconst | | + | {{dsc mem sconst|{{dsc small|{{c/core|inline constexpr std::partial_ordering}}}} unordered|nolink=true|a valid value indicating relationship with an incomparable value}} |
{{dsc end}} | {{dsc end}} | ||
===Conversions=== | ===Conversions=== | ||
− | {{tt|std::partial_ordering}} | + | {{tt|std::partial_ordering}} cannot be implicitly converted to other comparison category types, while both {{lc|std::strong_ordering}} and {{lc|std::weak_ordering}} are implicitly-convertible to {{tt|partial_ordering}}. |
− | {{ | + | ===Comparisons=== |
− | {{ | + | Comparison operators are defined between values of this type and literal {{c|0}}. This supports the expressions {{c|1=a <=> b == 0}} or {{c|1=a <=> b < 0}} that can be used to convert the result of a three-way comparison operator to a boolean relationship; see {{ltt|cpp/utility/compare/named comparison functions|std::is_eq}}, {{ltt|cpp/utility/compare/named comparison functions|std::is_lt}}, etc. |
− | {{ | + | |
− | {{ | + | {{cpp/hidden friend|plural=yes|{{tt|std::partial_ordering}}}} |
− | + | ||
− | + | ||
− | }} | + | |
− | + | The behavior of a program that attempts to compare a {{tt|partial_ordering}} with anything other than the integer literal {{c|0}} is undefined. | |
− | + | ||
− | + | ||
− | |||
− | |||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc fun | cpp/utility/compare/partial_ordering | inlinemem=true | title=operator{{= | + | {{dsc fun|cpp/utility/compare/partial_ordering|inlinemem=true|title=operator{{==}}<br>operator<<br>operator><br>operator<{{=}}<br>operator>{{=}}<br>operator<{{=}}>|compares with zero or a {{tt|partial_ordering}}}} |
{{dsc end}} | {{dsc end}} | ||
− | {{member | operator{{==}} | 2= | + | {{member|operator{{==}}|2= |
− | {{ | + | {{dcl begin}} |
− | friend constexpr bool operator==(partial_ordering v, /*unspecified*/ u | + | {{dcl|num=1|1= |
− | + | friend constexpr bool operator==( partial_ordering v, /*unspecified*/ u ) noexcept; | |
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr bool | ||
+ | operator==( partial_ordering v, partial_ordering w ) noexcept = default; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | | + | {{par|v, w|{{tt|std::partial_ordering}} values to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if {{tt|v}} is {{tt|equivalent}}, {{c|false}} if {{tt|v}} is {{tt|less}}, {{tt|greater}}, or {{tt|unordered}} | + | @1@ {{c|true}} if {{tt|v}} is {{tt|equivalent}}, {{c|false}} if {{tt|v}} is {{tt|less}}, {{tt|greater}}, or {{tt|unordered}} |
+ | @2@ {{c|true}} if both parameters hold the same value, {{c|false}} otherwise | ||
}} | }} | ||
− | {{member | operator | + | {{member|operator<|2= |
− | {{ | + | {{dcl begin}} |
− | friend constexpr bool operator | + | {{dcl|num=1|1= |
− | + | friend constexpr bool operator<( partial_ordering v, /*unspecified*/ u ) noexcept; | |
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr bool operator<( /*unspecified*/ u, partial_ordering v ) noexcept; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|std::partial_ordering }} value to check}} | + | {{par|v|a {{tt|std::partial_ordering}} value to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if {{tt|v}} is {{tt| | + | @1@ {{c|true}} if {{tt|v}} is {{tt|less}}, and {{c|false}} if {{tt|v}} is {{tt|greater}}, {{tt|equivalent}}, or {{tt|unordered}} |
+ | @2@ {{c|true}} if {{tt|v}} is {{tt|greater}}, and {{c|false}} if {{tt|v}} is {{tt|less}}, {{tt|equivalent}}, or {{tt|unordered}} | ||
}} | }} | ||
− | {{member | operator< | 2= | + | {{member|operator<{{=}}|2= |
− | {{ | + | {{dcl begin}} |
− | friend constexpr bool operator<(partial_ordering v, /*unspecified*/ u | + | {{dcl|num=1|1= |
− | + | friend constexpr bool operator<=( partial_ordering v, /*unspecified*/ u ) noexcept; | |
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr bool operator<=( /*unspecified*/ u, partial_ordering v ) noexcept; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|std::partial_ordering }} value to check}} | + | {{par|v|a {{tt|std::partial_ordering}} value to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if {{tt|v}} is {{tt|less}}, and {{c|false}} if {{tt|v}} is {{tt|greater}} | + | @1@ {{c|true}} if {{tt|v}} is {{tt|less}} or {{tt|equivalent}}, and {{c|false}} if {{tt|v}} is {{tt|greater}} or {{tt|unordered}} |
+ | @2@ {{c|true}} if {{tt|v}} is {{tt|greater}} or {{tt|equivalent}}, and {{c|false}} if {{tt|v}} is {{tt|less}} or {{tt|unordered}} | ||
}} | }} | ||
− | {{member | operator& | + | {{member|operator>|2= |
− | {{ | + | {{dcl begin}} |
− | friend constexpr bool operator | + | {{dcl|num=1|1= |
− | + | friend constexpr bool operator>( partial_ordering v, /*unspecified*/ u ) noexcept; | |
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr bool operator>( /*unspecified*/ u, partial_ordering v ) noexcept; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|std::partial_ordering }} value to check}} | + | {{par|v|a {{tt|std::partial_ordering}} value to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if {{tt|v}} is {{tt|less}} | + | @1@ {{c|true}} if {{tt|v}} is {{tt|greater}}, and {{c|false}} if {{tt|v}} is {{tt|less}}, {{tt|equivalent}}, or {{tt|unordered}} |
+ | @2@ {{c|true}} if {{tt|v}} is {{tt|less}}, and {{c|false}} if {{tt|v}} is {{tt|greater}}, {{tt|equivalent}}, or {{tt|unordered}} | ||
}} | }} | ||
− | {{member | operator> | 2= | + | {{member|operator>{{=}}|2= |
− | {{ | + | {{dcl begin}} |
− | friend constexpr bool operator>(partial_ordering v, /*unspecified*/ u | + | {{dcl|num=1|1= |
− | + | friend constexpr bool operator>=( partial_ordering v, /*unspecified*/ u ) noexcept; | |
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr bool operator>=( /*unspecified*/ u, partial_ordering v ) noexcept; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|std::partial_ordering }} value to check}} | + | {{par|v|a {{tt|std::partial_ordering}} value to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if {{tt|v}} is {{tt|greater}}, and {{c|false}} if {{tt|v}} is {{tt|less}} | + | @1@{{c|true}} if {{tt|v}} is {{tt|greater}} or {{tt|equivalent}}, and {{c|false}} if {{tt|v}} is {{tt|less}} or {{tt|unordered}} |
+ | @2@{{c|true}} if {{tt|v}} is {{tt|less}} or {{tt|equivalent}}, and {{c|false}} if {{tt|v}} is {{tt|greater}} or {{tt|unordered}} | ||
}} | }} | ||
− | + | {{member|operator<{{=}}>|2= | |
− | {{member | operator | + | {{dcl begin}} |
− | {{ | + | {{dcl|num=1|1= |
− | friend constexpr | + | friend constexpr partial_ordering operator<=>( partial_ordering v, /*unspecified*/ u ) noexcept; |
− | + | ||
}} | }} | ||
+ | {{dcl|num=2|1= | ||
+ | friend constexpr partial_ordering operator<=>( /*unspecified*/ u, partial_ordering v ) noexcept; | ||
+ | }} | ||
+ | {{dcl end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | v | a {{tt|std::partial_ordering }} value to check}} | + | {{par|v|a {{tt|std::partial_ordering}} value to check}} |
− | {{par | u | an unused parameter of any type that accepts literal zero argument}} | + | {{par|u|an unused parameter of any type that accepts literal zero argument}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c| | + | @1@ {{c|v}}. |
+ | @2@ {{tt|greater}} if {{tt|v}} is {{tt|less}}, {{tt|less}} if {{tt|v}} is {{tt|greater}}, otherwise {{tt|v}}. | ||
}} | }} | ||
+ | |||
+ | ===Notes=== | ||
+ | The [[cpp/language/operator comparison#Three-way comparison|built-in {{tt|1= operator<=>}}]] between floating-point values uses this ordering: the positive zero and the negative zero compare {{tt|equivalent}}, but can be distinguished, and NaN values compare {{tt|unordered}} with any other value. | ||
===Example=== | ===Example=== | ||
Line 143: | Line 168: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/utility/compare/dsc strong_ordering}} | + | {{dsc inc|cpp/utility/compare/dsc strong_ordering}} |
− | {{dsc inc | cpp/utility/compare/dsc weak_ordering | + | {{dsc inc|cpp/utility/compare/dsc weak_ordering}} |
− | + | ||
− | + | ||
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|zh}} | + | {{langlinks|es|ja|ru|zh}} |
Latest revision as of 05:04, 12 July 2024
Defined in header <compare>
|
||
class partial_ordering; |
(since C++20) | |
The class type std::partial_ordering
is the result type of a three-way comparison that:
- Admits all six relational operators (
==
,!=
,<
,<=
,>
,>=
).
- Does not imply substitutability: if a is equivalent to b, f(a) may not be equivalent to f(b), where f denotes a function that reads only comparison-salient state that is accessible via the argument's public const members. In other words, equivalent values may be distinguishable.
- Admits incomparable values: a < b, a == b, and a > b may all be false.
Contents |
[edit] Constants
The type std::partial_ordering
has four valid values, implemented as const static data members of its type:
Name | Definition |
inline constexpr std::partial_ordering less [static] |
a valid value indicating less-than (ordered before) relationship (public static member constant) |
inline constexpr std::partial_ordering equivalent [static] |
a valid value indicating equivalence (neither ordered before nor ordered after) (public static member constant) |
inline constexpr std::partial_ordering greater [static] |
a valid value indicating greater-than (ordered after) relationship (public static member constant) |
inline constexpr std::partial_ordering unordered [static] |
a valid value indicating relationship with an incomparable value (public static member constant) |
[edit] Conversions
std::partial_ordering
cannot be implicitly converted to other comparison category types, while both std::strong_ordering and std::weak_ordering are implicitly-convertible to partial_ordering
.
[edit] Comparisons
Comparison operators are defined between values of this type and literal 0. This supports the expressions a <=> b == 0 or a <=> b < 0 that can be used to convert the result of a three-way comparison operator to a boolean relationship; see std::is_eq, std::is_lt, etc.
These functions are not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::partial_ordering
is an associated class of the arguments.
The behavior of a program that attempts to compare a partial_ordering
with anything other than the integer literal 0 is undefined.
operator==operator<operator>operator<=operator>=operator<=> |
compares with zero or a partial_ordering (function) |
operator==
friend constexpr bool operator==( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator==( partial_ordering v, partial_ordering w ) noexcept = default; |
(2) | |
Parameters
v, w | - | std::partial_ordering values to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
v
is equivalent
, false if v
is less
, greater
, or unordered
operator<
friend constexpr bool operator<( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<( /*unspecified*/ u, partial_ordering v ) noexcept; |
(2) | |
Parameters
v | - | a std::partial_ordering value to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
v
is less
, and false if v
is greater
, equivalent
, or unordered
v
is greater
, and false if v
is less
, equivalent
, or unordered
operator<=
friend constexpr bool operator<=( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<=( /*unspecified*/ u, partial_ordering v ) noexcept; |
(2) | |
Parameters
v | - | a std::partial_ordering value to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
v
is less
or equivalent
, and false if v
is greater
or unordered
v
is greater
or equivalent
, and false if v
is less
or unordered
operator>
friend constexpr bool operator>( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>( /*unspecified*/ u, partial_ordering v ) noexcept; |
(2) | |
Parameters
v | - | a std::partial_ordering value to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
v
is greater
, and false if v
is less
, equivalent
, or unordered
v
is less
, and false if v
is greater
, equivalent
, or unordered
operator>=
friend constexpr bool operator>=( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>=( /*unspecified*/ u, partial_ordering v ) noexcept; |
(2) | |
Parameters
v | - | a std::partial_ordering value to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
v
is greater
or equivalent
, and false if v
is less
or unordered
v
is less
or equivalent
, and false if v
is greater
or unordered
operator<=>
friend constexpr partial_ordering operator<=>( partial_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr partial_ordering operator<=>( /*unspecified*/ u, partial_ordering v ) noexcept; |
(2) | |
Parameters
v | - | a std::partial_ordering value to check
|
u | - | an unused parameter of any type that accepts literal zero argument |
Return value
greater
if v
is less
, less
if v
is greater
, otherwise v
.
[edit] Notes
The built-in operator<=>
between floating-point values uses this ordering: the positive zero and the negative zero compare equivalent
, but can be distinguished, and NaN values compare unordered
with any other value.
[edit] Example
This section is incomplete Reason: no example |
[edit] See also
(C++20) |
the result type of 3-way comparison that supports all 6 operators and is substitutable (class) |
(C++20) |
the result type of 3-way comparison that supports all 6 operators and is not substitutable (class) |