Title
Three-way comparison requiring strong ordering for floating-point types
Status
tentatively ready
Section
11.10.3 [class.spaceship]
Submitter
Richard Smith

Created on 2022-02-24.00:00:00 last changed 2 weeks ago

Messages

Date: 2022-11-20.07:54:16

Proposed resolution (approved by CWG 2022-11-11):

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, ...
Date: 2022-02-24.00:00:00

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 either
  • 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.
MyType().d <=> MyType().d is a valid expression.
History
Date User Action Args
2022-11-20 07:54:16adminsetstatus: open -> tentatively ready
2022-02-24 00:00:00admincreate
2022-02-18 07:47:23adminsetmessages: + msg6746