This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2024-11-11
[Accepted as a DR at the February, 2023 meeting.]
Consider:
struct MyType { int i; double d; std::strong_ordering operator<=> (const MyType& c) const = default; };
The defaulted three-way comparison operator is defined only if it is used, per 11.10.1 [class.compare.default] paragraph 1:
A comparison operator function for class C that is defaulted on its first declaration and is not defined as deleted is implicitly defined when it is odr-used or needed for constant evaluation.
The current rules make an odr-use of the three-way comparison operator ill-formed, but it would be preferable if it were deleted instead. In particular, 11.10.3 [class.spaceship] bullet 2.2 specifies
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.
This refers to bullets 1.2 and 1.3 of 11.10.3 [class.spaceship] paragraph 1:
The synthesized three-way comparison of type R (17.11.2 [cmp.categories]) of glvalues a and b of the same type is defined as follows:
- If a <=> b is usable (11.10.1 [class.compare.default]), static_cast<R>(a <=> b).
- Otherwise, if 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 < b is not usable, the synthesized three-way comparison is not defined.
- Otherwise, ...
However, a <=> b is actually usable, because 11.10.1 [class.compare.default] paragraph 3 defines:
A binary operator expression a @ b is usable if eitherMyType().d <=> MyType().d is a valid expression.
- a or b is of class or enumeration type and overload resolution (12.2 [over.match]) as applied to a @ b results in a usable candidate, or
- neither a nor b is of class or enumeration type and a @ b is a valid expression.
Proposed resolution (approved by CWG 2022-11-11) [SUPERSEDED]:
The synthesized three-way comparison of type R (17.11.2 [cmp.categories]) of glvalues a and b of the same type is defined as follows:
- If a <=> b is usable (11.10.1 [class.compare.default]) and
- if overload resolution for a direct-initialization of an object or reference of type R from a <=> b results in a usable candidate, static_cast<R>(a <=> b),
- otherwise, the synthesized three-way comparison is not defined.
- Otherwise, if 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 < b is not usable, the synthesized three-way comparison is not defined.
- Otherwise, ...
CWG 2023-02-06
A simplification of the wording is sought.
Proposed resolution (approved by CWG 2023-02-07):
The synthesized three-way comparison of type R (17.11.2 [cmp.categories]) of glvalues a and b of the same type is defined as follows:
- If a <=> b is usable (11.10.1 [class.compare.default]) and can be explicitly converted to R using static_cast, static_cast<R>(a <=> b).
- Otherwise, if 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 < b is not usable, the synthesized three-way comparison is not defined.
- Otherwise, ...