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


2547. Defaulted comparison operator function for non-classes

Section: 9.5.2  [dcl.fct.def.default]     Status: DR     Submitter: Jim X     Date: 2022-03-07

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

(See editorial issue 5337.)

Subclause 9.5.2 [dcl.fct.def.default] paragraph 1 specifies:

A function definition whose function-body is of the form = default ; is called an explicitly-defaulted definition. A function that is explicitly defaulted shall

There seem to be no further restrictions on which comparison operator functions are allowed to be defaulted. For example,

  enum E { };
  bool operator==(E, E) = default;  // well-formed?

Subclause 11.10.1 [class.compare.default] paragraph 1 applies only to comparison operator functions "for some class":

A defaulted comparison operator function (12.4.3 [over.binary]) for some class C shall be a non-template function that is

Proposed resolution [SUPERSEDED]:

  1. Change in 9.5.2 [dcl.fct.def.default] paragraph 1 as follows:
    A function definition whose function-body is of the form = default ; is called an explicitly-defaulted definition. A function that is explicitly defaulted shall
  2. Change in 11.10.1 [class.compare.default] paragraph 1 as follows:
    A defaulted comparison operator function (12.4.3 [over.binary]) for some class C shall be a non-template function that is
    • a non-static const non-volatile member of some class C having one parameter of type const C& and either no ref-qualifier or the ref-qualifier &, or
    • a friend of some class C having either two parameters of type const C& or two parameters of type C.

    Such a comparison operator function is termed a comparison operator function for class C. A comparison operator function for class C that is defaulted on its first declaration ...

CWG 2023-12-01

A defaulted comparison function for an incomplete class later declared a friend for that class should be made ill-formed.

Proposed resolution (approved by CWG 2023-12-15):

  1. Change in 9.5.2 [dcl.fct.def.default] paragraph 1 as follows:
    A function definition whose function-body is of the form = default ; is called an explicitly-defaulted definition. A function that is explicitly defaulted shall
  2. Change in 11.10.1 [class.compare.default] paragraph 1 as follows:

    A defaulted comparison operator function (12.4.3 [over.binary]) for some class C shall be a non-template function that is
    • is a non-static member or friend of some class C,
    • is defined as defaulted in C or in a context where C is complete, and
    • either has two parameters of type const C& or two parameters of type C, where the implicit object parameter (if any) is considered to be the first parameter.
    Such a comparison operator function is termed a defaulted comparison operator function for class C. Name lookups in the implicit definition (9.5.2 [dcl.fct.def.default]) of a comparison operator function are performed from a context equivalent to its function-body. A definition of a comparison operator as defaulted that appears in a class shall be the first declaration of that function. [ Example:
      struct S;
      bool operator==(S, S) = default;  // error: S is not complete
      struct S {
        friend bool operator==(S, const S&) = default; // error: parameters of different types
      };
      enum E { };
      bool operator==(E, E) = default;  // error: not a member or friend of a class
    
    -- end example ]