Title
Defaulted comparison operator function for non-classes
Status
drwp
Section
9.5.2 [dcl.fct.def.default]
Submitter
Jim X

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

Messages

Date: 2023-12-16.00:18:01

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 ]
Date: 2023-12-03.16:28:48

CWG 2023-12-01

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

Date: 2023-12-03.16:28:48

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 ...

Date: 2024-03-15.00:00:00

[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
  • be a special member function or a comparison operator function (12.4.3 [over.binary]), and
  • not have default arguments.

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
  • a non-static const non-volatile member of C having one parameter of type const C& and either no ref-qualifier or the ref-qualifier &, or
  • a friend of C having either two parameters of type const C& or two parameters of type C.
History
Date User Action Args
2024-07-20 13:52:34adminsetstatus: dr -> drwp
2024-04-05 21:43:46adminsetstatus: ready -> dr
2024-03-20 14:10:31adminsetstatus: tentatively ready -> ready
2023-12-16 00:18:01adminsetstatus: review -> tentatively ready
2023-12-03 16:28:48adminsetmessages: + msg7548
2023-12-03 16:28:48adminsetmessages: + msg7547
2023-06-15 19:36:24adminsetstatus: open -> review
2022-03-07 00:00:00admincreate
2022-02-18 07:47:23adminsetmessages: + msg6759