Files
2025-10-25 03:02:53 +03:00

6.3 KiB

[class.compare.default]

11 Classes [class]

11.10 Comparisons [class.compare]

11.10.1 Defaulted comparison operator functions [class.compare.default]

1

#

A defaulted comparison operator function ([over.binary]) shall be a non-template function that

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 termeda defaulted comparison operator function for class C.

Name lookups and access checks in the implicit definition ([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 1: struct S;bool operator==(S, S) = default; // error: S is not completestruct 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]

2

#

A defaulted <=> or == operator function for class Cis defined as deleted if any non-static data member of C is of reference type orC has variant members ([class.union.anon]).

3

#

A binary operator expression a @ b isusable if either

a or b is of class or enumeration type and overload resolution ([over.match]) as applied to a @ b results in a usable candidate, or

neither a nor b is of class or enumeration type anda @ b is a valid expression.

4

#

If the member-specification does not explicitly declare any member or friend named operator==, an == operator function is declared implicitly for each three-way comparison operator function defined as defaulted in the member-specification, with the same access and function-definition and in the same class scope as the respective three-way comparison operator function, except that the return type is replaced with bool and the declarator-id is replaced with operator==.

[Note 1:

Such an implicitly-declared == operator for a class X is defined as defaulted in the definition of X and has the same parameter-declaration-clause and trailing requires-clause as the respective three-way comparison operator.

It is declared with friend, virtual, constexpr, or consteval if the three-way comparison operator function is so declared.

If the three-way comparison operator function has no noexcept-specifier, the implicitly-declared == operator function has an implicit exception specification ([except.spec]) that can differ from the implicit exception specification of the three-way comparison operator function.

— end note]

[Example 2: template struct X {friend constexpr std::partial_ordering operator<=>(X, X) requires (sizeof(T) != 1) = default; // implicitly declares: friend constexpr bool operator==(X, X) requires (sizeof(T) != 1) = default;nodiscard virtual std::strong_ordering operator<=>(const X&) const = default; // implicitly declares: nodiscard virtual bool operator==(const X&) const = default;}; — end example]

[Note 2:

The == operator function is declared implicitly even if the defaulted three-way comparison operator function is defined as deleted.

— end note]

5

#

The direct base class subobjects of C, in the order of their declaration in the base-specifier-list of C, followed by the non-static data members of C, in the order of their declaration in the member-specification of C, form a list of subobjects.

In that list, any subobject of array type is recursively expanded to the sequence of its elements, in the order of increasing subscript.

Let xi be an lvalue denoting the ith element in the expanded list of subobjects for an object x (of length n), where xi is formed by a sequence of derived-to-base conversions ([over.best.ics]), class member access expressions ([expr.ref]), and array subscript expressions ([expr.sub]) applied to x.