Skip to content

Commit be1dfcd

Browse files
committed
P1630R1 Spaceship needs a tune-up
Definition of 'usable function' not added: it will be added by CWG motion 6 (P1186R3) instead.
1 parent 67db942 commit be1dfcd

File tree

3 files changed

+101
-49
lines changed

3 files changed

+101
-49
lines changed

source/classes.tex

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6661,6 +6661,12 @@
66616661
\item a friend of \tcode{C} having two parameters of type \tcode{const C\&}.
66626662
\end{itemize}
66636663

6664+
\pnum
6665+
A defaulted comparison operator function for class \tcode{C}
6666+
is defined as deleted if
6667+
any non-static data member of \tcode{C} is of reference type or
6668+
\tcode{C} is a union-like class\iref{class.union.anon}.
6669+
66646670
\pnum
66656671
If the class definition
66666672
does not explicitly declare an \tcode{==} operator function,
@@ -6697,15 +6703,22 @@
66976703
given a glvalue \tcode{x} of type \tcode{const C}, either:
66986704
\begin{itemize}
66996705
\item
6700-
\tcode{C} is a non-class type and \tcode{x <=> x} is a valid expression
6701-
of type \tcode{std::strong_ordering} or \tcode{std::strong_equality}, or
6706+
\tcode{C} is a non-class type and
6707+
\tcode{x <=> x} is a valid expression
6708+
of type \tcode{std::strong_ordering} or \tcode{std::strong_equality}, or
6709+
67026710
\item
6703-
\tcode{C} is a class type with an \tcode{==} operator
6704-
defined as defaulted in the definition of \tcode{C},
6705-
\tcode{x == x} is well-formed when contextually converted to \tcode{bool},
6706-
all of \tcode{C}'s base class subobjects and non-static data members
6707-
have strong structural equality, and
6708-
\tcode{C} has no \tcode{mutable} or \tcode{volatile} subobjects.
6711+
\tcode{C} is a class type
6712+
where all of the following hold:
6713+
\begin{itemize}
6714+
\item All of \tcode{C}'s base class subobjects and non-static data members
6715+
have strong structural equality.
6716+
\item \tcode{C} has no mutable or volatile non-static data members.
6717+
\item At the end of the definition of \tcode{C},
6718+
overload resolution performed for the expression \tcode{x == x} succeeds and
6719+
finds either a friend or public member \tcode{==} operator
6720+
that is defined as defaulted in the definition of \tcode{C}.
6721+
\end{itemize}
67096722
\end{itemize}
67106723

67116724
\pnum
@@ -6760,15 +6773,12 @@
67606773
with parameters \tcode{x} and \tcode{y} is defined as deleted if
67616774
\begin{itemize}
67626775
\item
6763-
overload resolution\iref{over.match}, as applied to \tcode{x == y}
6764-
(also considering synthesized candidates
6765-
with reversed order of parameters\iref{over.match.oper}),
6766-
results in an ambiguity or a function
6767-
that is deleted or inaccessible from the operator function, or
6776+
overload resolution\iref{over.match}, as applied to \tcode{x == y},
6777+
does not result in a usable function, or
67686778
\item
6769-
\tcode{x == y} cannot be contextually converted to \tcode{bool}.
6779+
\tcode{x == y} is not a prvalue of type \tcode{bool}.
67706780
\end{itemize}
6771-
Otherwise, the operator function yields \tcode{(x == y) ?\ false :\ true}.
6781+
Otherwise, the operator function yields \tcode{!(x == y)}.
67726782

67736783
\pnum
67746784
\begin{example}
@@ -6777,7 +6787,7 @@
67776787
int i;
67786788
friend bool operator==(const D& x, const D& y) = default;
67796789
// OK, returns \tcode{x.i == y.i}
6780-
bool operator!=(const D& z) const = default; // OK, returns \tcode{(*this == z) ?\ false :\ true}
6790+
bool operator!=(const D& z) const = default; // OK, returns \tcode{!(*this == z)}
67816791
};
67826792
\end{codeblock}
67836793
\end{example}
@@ -6874,10 +6884,8 @@
68746884
\begin{itemize}
68756885
\item
68766886
overload resolution\iref{over.match},
6877-
as applied to \tcode{x <=> y}
6878-
results in an ambiguity
6879-
or a function that is deleted or inaccessible from the operator function,
6880-
or
6887+
as applied to \tcode{x <=> y},
6888+
does not result in a usable function, or
68816889

68826890
\item
68836891
the operator \tcode{@}

source/compatibility.tex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,37 @@
20042004
};
20052005
\end{codeblock}
20062006

2007+
\rSec2[diff.cpp17.over]{\ref{over}: overloading}
2008+
2009+
\diffref{over.match.oper}
2010+
\change
2011+
Equality and inequality expressions can now find
2012+
reversed and rewritten candidates.
2013+
\rationale
2014+
Improve consistency of equality with three-way comparison
2015+
and make it easier to write the full complement of equality operations.
2016+
\effect
2017+
Equality and inequality expressions between two objects of different types,
2018+
where one is convertible to the other,
2019+
could invoke a different operator.
2020+
Equality and inequality expressions between two objects of the same type
2021+
could become ambiguous.
2022+
\begin{codeblock}
2023+
struct A {
2024+
operator int() const;
2025+
};
2026+
2027+
bool operator==(A, int); // \#1
2028+
// \#2 is built-in candidate: \tcode{bool operator==(int, int);}
2029+
// \#3 is built-in candidate: \tcode{bool operator!=(int, int);}
2030+
2031+
int check(A x, A y) {
2032+
return (x == y) + // ill-formed; previously well-formed
2033+
(10 == x) + // calls \#1, previously selected \#2
2034+
(10 != x); // calls \#1, previously selected \#3
2035+
}
2036+
\end{codeblock}
2037+
20072038
\rSec2[diff.cpp17.temp]{\ref{temp}: templates}
20082039

20092040
\diffref{temp.names}

source/overloading.tex

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,15 @@
10711071
that is not a function template specialization.
10721072
\end{itemize}
10731073

1074+
\item
1075+
The rewritten candidate set is determined as follows:
1076+
\begin{itemize}
10741077
\item
10751078
For the relational\iref{expr.rel} operators,
10761079
the rewritten candidates include
10771080
all member, non-member, and built-in candidates
1078-
for the operator \tcode{<=>}
1079-
for which the rewritten expression
1080-
\tcode{(x <=> y) @ 0} is well-formed using that \tcode{operator<=>}.
1081+
for the expression \tcode{x <=> y}.
1082+
\item
10811083
For the
10821084
relational\iref{expr.rel} and
10831085
three-way comparison\iref{expr.spaceship}
@@ -1086,29 +1088,27 @@
10861088
a synthesized candidate,
10871089
with the order of the two parameters reversed,
10881090
for each member, non-member, and built-in candidate
1089-
for the operator \tcode{<=>}
1090-
for which the rewritten expression
1091-
\tcode{0 @ (y <=> x)} is well-formed using that \tcode{operator<=>}.
1091+
for the expression
1092+
\tcode{y <=> x}.
1093+
\item
10921094
For the \tcode{!=} operator\iref{expr.eq},
10931095
the rewritten candidates
10941096
include all member, non-member, and built-in candidates
1095-
for the operator \tcode{==}
1096-
for which the rewritten expression \tcode{(x == y)} is well-formed
1097-
when contextually converted to \tcode{bool} using that operator \tcode{==}.
1097+
for the expression \tcode{x == y}.
1098+
\item
10981099
For the equality operators,
10991100
the rewritten candidates also include a synthesized candidate,
11001101
with the order of the two parameters reversed,
1101-
for each member, non-member, and built-in candidate for the operator \tcode{==}
1102-
for which the rewritten expression \tcode{(y == x)} is well-formed
1103-
when contextually converted to \tcode{bool} using that operator \tcode{==}.
1102+
for each member, non-member, and built-in candidate
1103+
for the expression \tcode{y == x}.
1104+
\item
1105+
For all other operators, the rewritten candidate set is empty.
1106+
\end{itemize}
11041107
\begin{note}
11051108
A candidate synthesized from a member candidate has its implicit
11061109
object parameter as the second parameter, thus implicit conversions
11071110
are considered for the first, but not for the second, parameter.
11081111
\end{note}
1109-
In each case, rewritten candidates are not considered
1110-
in the context of the rewritten expression.
1111-
For all other operators, the rewritten candidate set is empty.
11121112
\end{itemize}
11131113

11141114
\pnum
@@ -1159,26 +1159,39 @@
11591159
\end{example}
11601160

11611161
\pnum
1162-
If a rewritten candidate is selected by overload resolution
1163-
for a relational or three-way comparison operator \tcode{@},
1162+
If a rewritten \tcode{operator<=>} candidate
1163+
is selected by overload resolution
1164+
for an operator \tcode{@},
11641165
\tcode{x @ y}
1165-
is interpreted as the rewritten expression:
1166+
is interpreted as
11661167
\tcode{0 @ (y <=> x)}
11671168
if the selected candidate is a synthesized candidate
11681169
with reversed order of parameters,
11691170
or \tcode{(x <=> y) @ 0} otherwise,
11701171
using the selected rewritten \tcode{operator<=>} candidate.
1171-
If a rewritten candidate is selected by overload resolution
1172-
for a \tcode{!=} operator,
1173-
\tcode{x != y} is interpreted as \tcode{(y == x) ?\ false :\ true}
1174-
if the selected candidate is a synthesized candidate
1175-
with reversed order of parameters, or
1176-
\tcode{(x == y) ?\ false :\ true} otherwise,
1177-
using the selected rewritten \tcode{operator==} candidate.
1178-
If a rewritten candidate is selected by overload resolution
1179-
for an \tcode{==} operator,
1180-
\tcode{x == y} is interpreted as \tcode{(y == x) ?\ true :\ false}
1181-
using the selected rewritten \tcode{operator==} candidate.
1172+
Rewritten candidates for the operator \tcode{@}
1173+
are not considered in the context of the resulting expression.
1174+
1175+
\pnum
1176+
If a rewritten \tcode{operator==} candidate
1177+
is selected by overload resolution
1178+
for an operator \tcode{@},
1179+
its return type shall be \cv{} \tcode{bool}, and
1180+
\tcode{x @ y} is interpreted as:
1181+
\begin{itemize}
1182+
\item
1183+
if \tcode{@} is \tcode{!=}
1184+
and the selected candidate is a synthesized candidate
1185+
with reversed order of parameters,
1186+
\tcode{!(y == x)},
1187+
\item
1188+
otherwise, if \tcode{@} is \tcode{!=},
1189+
\tcode{!(x == y)},
1190+
\item
1191+
otherwise (when \tcode{@} is \tcode{==}),
1192+
\tcode{y == x},
1193+
\end{itemize}
1194+
in each case using the selected rewritten \tcode{operator==} candidate.
11821195

11831196
\pnum
11841197
If a built-in candidate is selected by overload resolution, the

0 commit comments

Comments
 (0)