N4532
Jens Maurer <Jens.Maurer@gmx.net>
2015-05-22

N4532: Proposed wording for default comparisons

Introduction

This paper presents proposed wording to implement defaulted comparisons as presented in N4475 "Default comparisons (R2)" by Bjarne Stroustrup.

Open Issues

Wording

Add at the end of 3.9.2 [basic.compound]:
A type cv T supports comparison by subobject if T is an array type, or is a non-union class type that satisfies the following constraints: T supports operator op if overload resolution (13.3.1.2 [over.match.oper]) finds a viable function for the expression x op y, where x and y are lvalues of type T.

T supports equality comparison by subobject if T supports comparison by subobject and does not support operator== and operator!=.

T supports relational comparison by subobject if T supports equality comparison by subobject and does not support operator <, operator <=, operator >, and operator >=.

Change in 9 [class] paragraph 7 bullet 6:
Change in 9.2 [class.mem] paragraph 1:
The member-specification in a class definition declares the full set of members of the class; no member can be added elsewhere. A direct member of a class is a member of that class that was first declared within the class's member-specification. ...
Add a new section 9.10 [class.oper]:
9.10 Operators [class.oper]

[ Note: This section specifies the meaning of relational (5.9 [expr.rel]) or equality (5.10 [expr.eq]) operators applied to values of class or array type. ]

Comparing for equality two objects x and y that have the same class or array type T (ignoring cv-qualification) is defined as follows:

Comparing two objects x and y that have the same class or array type T (ignoring cv-qualification) is defined as follows:

[ Example:
struct B {
  int i = 0;
};

struct S : B {
  int j = 1;
};

struct T : S {
   bool operator>(T) = delete;
};

void f()
{
  B b;
  S s1, s2;
  s2.j = 2;
  s1 == s1;      // yields true
  s1 != s2;      // yields true
  s1 < s1;       // yields false
  s1 < s2;       // yields true
  s1 == b;       // error: not the same class type
  T() == T();    // yields true
  T() < T();     // error: operator> is user-declared
}
-- end example ]
Add a paragraph after 13.3.1.2 [over.match.oper] paragraph 7:
If overload resolution yields no viable function (13.3.2 [over.match.viable]) for a relational (5.9 [expr.rel]) or equality (5.10 [expr.eq]) operator and the operands are of the same class or array type T (ignoring cv-qualification), then:

Acknowledgements

Thanks to Richard Smith for valuable input, and the majority of the drafting presented above. All errors are mine, of course. Thanks to Oleg Smolsky for acquiring a paper number and some discussions on earlier wording.