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

136 lines
6.3 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.compare.default]
# 11 Classes [[class]](./#class)
## 11.10 Comparisons [[class.compare]](class.compare#default)
### 11.10.1 Defaulted comparison operator functions [class.compare.default]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6562)
A defaulted comparison operator function ([[over.binary]](over.binary "12.4.3Binary operators"))
shall be a non-template function
that
- [(1.1)](#1.1)
is a non-static member or friend of some class C,
- [(1.2)](#1.2)
is defined as defaulted in C or in
a context where C is complete, and
- [(1.3)](#1.3)
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[.](#1.sentence-1)
Such a comparison operator function is termeda defaulted comparison operator function for class C[.](#1.sentence-2)
Name lookups and access checks in
the implicit definition ([[dcl.fct.def.default]](dcl.fct.def.default "9.6.2Explicitly-defaulted functions"))
of a comparison operator function
are performed from a context equivalent to
its [*function-body*](dcl.fct.def.general#nt:function-body "9.6.1General[dcl.fct.def.general]")[.](#1.sentence-3)
A definition of a comparison operator as
defaulted that appears in a class shall be the first declaration
of that function[.](#1.sentence-4)
[*Example [1](#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](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6604)
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]](class.union.anon "11.5.2Anonymous unions"))[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6612)
A binary operator expression a @ b is[*usable*](#def:usable,binary_operator_expression "11.10.1Defaulted comparison operator functions[class.compare.default]") if either
- [(3.1)](#3.1)
a or b is of class or enumeration type and
overload resolution ([[over.match]](over.match "12.2Overload resolution")) as applied to a @ b results in a usable candidate, or
- [(3.2)](#3.2)
neither a nor b is of class or enumeration type anda @ b is a valid expression[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6627)
If the [*member-specification*](class.mem.general#nt:member-specification "11.4.1General[class.mem.general]") 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*](class.mem.general#nt:member-specification "11.4.1General[class.mem.general]"),
with the same access and [*function-definition*](dcl.fct.def.general#nt:function-definition "9.6.1General[dcl.fct.def.general]") 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*](dcl.decl.general#nt:declarator-id "9.3.1General[dcl.decl.general]") is replaced with operator==[.](#4.sentence-1)
[*Note [1](#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*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6Functions[dcl.fct]") and
trailing [*requires-clause*](temp.pre#nt:requires-clause "13.1Preamble[temp.pre]") as
the respective three-way comparison operator[.](#4.sentence-2)
It is declared with friend, virtual, constexpr,
or consteval if
the three-way comparison operator function is so declared[.](#4.sentence-3)
If the three-way comparison operator function
has no [*noexcept-specifier*](except.spec#nt:noexcept-specifier "14.5Exception specifications[except.spec]"),
the implicitly-declared == operator function
has an implicit exception specification ([[except.spec]](except.spec "14.5Exception specifications")) that
can differ from the implicit exception specification of
the three-way comparison operator function[.](#4.sentence-4)
— *end note*]
[*Example [2](#example-2)*: template<typename T> 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](#note-2)*:
The == operator function is declared implicitly even if
the defaulted three-way comparison operator function
is defined as deleted[.](#4.sentence-5)
— *end note*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L6673)
The direct base class subobjects of C,
in the order of their declaration in the [*base-specifier-list*](class.derived.general#nt:base-specifier-list "11.7.1General[class.derived.general]") of C,
followed by the non-static data members of C,
in the order of their declaration in the [*member-specification*](class.mem.general#nt:member-specification "11.4.1General[class.mem.general]") of C,
form a list of subobjects[.](#5.sentence-1)
In that list, any subobject of array type is recursively expanded
to the sequence of its elements, in the order of increasing subscript[.](#5.sentence-2)
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]](over.best.ics "12.2.4.2Implicit conversion sequences")),
class member access expressions ([[expr.ref]](expr.ref "7.6.1.5Class member access")), and
array subscript expressions ([[expr.sub]](expr.sub "7.6.1.2Subscripting")) applied to x[.](#5.sentence-3)