This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 114a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-18


2539. Three-way comparison requiring strong ordering for floating-point types

Section: 11.10.3  [class.spaceship]     Status: C++23     Submitter: Richard Smith     Date: 2022-02-24

[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:

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
MyType().d <=> MyType().d 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:

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: