Title
Defaulted secondary comparison operators defined as deleted
Status
drwp
Section
11.10.4 [class.compare.secondary]
Submitter
Jim X

Created on 2022-03-07.00:00:00 last changed 5 months ago

Messages

Date: 2024-03-20.00:29:35

Proposed resolution (approved by CWG 2024-03-20):

Change in 11.10.4 [class.compare.secondary] paragraph 2 as follows:
The operator function with parameters x and y is defined as deleted if
  • a first overload resolution (12.2 [over.match]), as applied to x @ y,
    • does not result in a usable candidate, or
    • the selected candidate selected by overload resolution is not a rewritten candidate. , or
  • a second overload resolution for the expression resulting from the interpretation of x @ y using the selected rewritten candidate (12.2.2.3 [over.match.oper]) does not result in a usable candidate (for example, that expression might be (x <=> y) @ 0), or
  • x @ y cannot be implicitly converted to bool.

In any of the two overload resolutions above, the defaulted operator function is not considered as a candidate for the @ operator. Otherwise, the operator function yields x @ y. The defaulted operator function is not considered as a candidate in the overload resolution for the @ operator.

Date: 2024-03-15.00:00:00

[Accepted as a DR at the March, 2024 meeting.]

(See also editorial issues 5335 and 5336.)

Consider the example in 11.10.4 [class.compare.secondary] paragraph 3:

  struct HasNoLessThan { };
  struct C {
    friend HasNoLessThan operator<=>(const C&, const C&);
    bool operator<(const C&) const = default;  // OK, function is deleted
  };

While the comment may reflect the intent, it does not follow from the wording. 11.10.4 [class.compare.secondary] paragraph 2 specifies:

The operator function with parameters x and y is defined as deleted if
  • overload resolution (12.2 [over.match]), as applied to x @ y, does not result in a usable candidate, or
  • the candidate selected by overload resolution is not a rewritten candidate.

Otherwise, the operator function yields x @ y. The defaulted operator function is not considered as a candidate in the overload resolution for the @ operator.

Overload resolution applied to x < y results in a usable candidate operator<=> (12.2.1 [over.match.general]) and that candidate is a rewritten candidate (12.2.2.3 [over.match.oper] bullet 3.4), thus operator< in the above example is not deleted. However, its definition is ill-formed, because the rewrite (x <=> y) < 0 is ill-formed (12.2.2.3 [over.match.oper] paragraph 8).

There is implementation divergence.

Subclause 11.10.3 [class.spaceship] paragraph 1 seems to prefer an ill-formed program for similar synthesized situations:

[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. —end note]
History
Date User Action Args
2024-07-20 13:52:34adminsetstatus: dr -> drwp
2024-04-05 21:43:46adminsetstatus: ready -> dr
2024-03-20 00:29:35adminsetstatus: tentatively ready -> ready
2023-12-02 15:54:05adminsetstatus: review -> tentatively ready
2023-06-15 19:36:24adminsetstatus: open -> review
2022-03-07 00:00:00admincreate
2022-02-18 07:47:23adminsetmessages: + msg6757