Skip to content

Commit 21f5a8a

Browse files
bernhardmgrubersponce
authored andcommitted
Revise slides on spaceship
1 parent a342592 commit 21f5a8a

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

talk/expert/cpp20spaceship.tex

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
\frametitle{The burden of comparison operators}
55
\begin{block}{Motivation}
66
\begin{itemize}
7-
\item One often needs \mintinline{cpp}{operator<} for a home-made class
7+
\item One often needs \mintinline{cpp}{operator<} for a user-defined class
88
\begin{itemize}
99
\item e.g.\ when sorting a \mintinline{cpp}{std::vector}
1010
\item e.g.\ when using it as a key for \mintinline{cpp}{std::set} or \mintinline{cpp}{std::map}
@@ -14,7 +14,7 @@
1414
\item \mintinline{cpp}{operator>}, \mintinline{cpp}{operator>=}, \mintinline{cpp}{operator<=}, ...
1515
\item often implemented reusing e.g.\ \mintinline{cpp}{operator<} and \mintinline{cpp}{operator==}
1616
\end{itemize}
17-
\item All these should be defined as free functions, optionally friends
17+
\item Should be defined as hidden friend functions
1818
\item Much boilerplate code to write. Too much...
1919
\end{itemize}
2020
\end{block}
@@ -38,7 +38,7 @@
3838
\begin{itemize}
3939
\item similar to \mintinline{cpp}{std::strcmp}
4040
\end{itemize}
41-
\item greater, lower or equal to \mintinline{cpp}{0} means respectively \emph{lower than}, \emph{greater than} and \emph{equivalent to}
41+
\item lower, greater, or equal to \mintinline{cpp}{0} means respectively \emph{lower than}, \emph{greater than} and \emph{equivalent to}
4242
\item It is provided by default for all built-in types and many types in the standard library
4343
\end{itemize}
4444
\end{exampleblock}
@@ -50,40 +50,41 @@
5050
\begin{cppcode*}{}
5151
template <typename T>
5252
void three_way_compare( T lhs, T rhs ) {
53-
auto res = ( lhs <=> rhs );
54-
std::cout << "(" << lhs << "<=>" << rhs << ") : "
53+
auto res = lhs <=> rhs;
54+
std::cout << lhs << "<=>" << rhs << ": "
5555
<< (res<0) << (res==0) << (res>0)
56-
<< std::endl;
56+
<< '\n';
5757
}
5858
int main() {
59-
three_way_compare(1, 2); // (1<=>2): 100
60-
three_way_compare(2, 2); // (2<=>2): 010
61-
three_way_compare(2, 1); // (2<=>1): 001
59+
three_way_compare(1, 2); // 1<=>2: 100
60+
three_way_compare(2, 2); // 2<=>2: 010
61+
three_way_compare(2, 1); // 2<=>1: 001
6262
}
6363
\end{cppcode*}
6464
\end{exampleblock}
6565
\end{frame}
6666
6767
\begin{frame}[fragile]
6868
\frametitlecpp[20]{Different kinds of ordering}
69-
\begin{block}{About the return type of \texttt{operator<=>}}
69+
\begin{block}{The return type of \texttt{operator<=>}}
7070
\begin{itemize}
7171
\item for integers \mintinline{cpp}{operator<=>} returns \mintinline{cpp}{std::strong_ordering}
72-
\item \mintinline{cpp}{partial_ordering} and \mintinline{cpp}{weak_ordering} also exist
72+
\item \mintinline{cpp}{weak_ordering} and \mintinline{cpp}{partial_ordering} also exist
7373
\end{itemize}
7474
\end{block}
7575
\begin{exampleblock}{3 types of ordering}
7676
\begin{description}[partial]
7777
\item[strong] exactly one test among \mintinline{cpp}{<0}, \mintinline{cpp}{==0}, and \mintinline{cpp}{>0} will return \mintinline{cpp}{true}
78-
\item[partial] like strong but all tests may return \mintinline{cpp}{false}
79-
\begin{itemize}
80-
\item e.g.\ compare a floating point to \mintinline{cpp}{NaN}
81-
\end{itemize}
8278
\item[weak] like strong but two \emph{equivalent} values may differ
8379
\begin{itemize}
8480
\item they are however \emph{equivalent} for ranking
8581
\item e.g.\ rational numbers $2/3$ and $4/6$
8682
\end{itemize}
83+
\item[partial] like weak but some values are incomparable
84+
\begin{itemize}
85+
\item for some values all tests may return \mintinline{cpp}{false}
86+
\item e.g.\ compare a floating point to \mintinline{cpp}{NaN}
87+
\end{itemize}
8788
\end{description}
8889
\end{exampleblock}
8990
\end{frame}
@@ -104,15 +105,15 @@
104105
}
105106
};
106107
int main() {
107-
// floats use partial_ordering
108-
three_way_compare(+0., -0.); // (0<=>-0) : 010
109-
three_way_compare(0./1., 1./0.); // (0<=>inf) : 100
110-
three_way_compare(0., 0./0.); // (0<=>-nan) : 000
111-
112108
// Ratio uses weak_ordering
113-
three_way_compare(Ratio{1, 2}, Ratio{2, 3}); // (1/2<=>2/3) : 100
114-
three_way_compare(Ratio{2, 3}, Ratio{4, 6}); // (2/3<=>4/6) : 010
115-
three_way_compare(Ratio{2, 3}, Ratio{1, 2}); // (2/3<=>1/2) : 001
109+
three_way_compare(Ratio{1, 2}, Ratio{2, 3}); // 1/2<=>2/3 : 100
110+
three_way_compare(Ratio{2, 3}, Ratio{4, 6}); // 2/3<=>4/6 : 010
111+
three_way_compare(Ratio{2, 3}, Ratio{1, 2}); // 2/3<=>1/2 : 001
112+
113+
// floats use partial_ordering
114+
three_way_compare(+0., -0.); // 0<=>-0 : 010
115+
three_way_compare(0., 1./0.); // 0<=>inf : 100
116+
three_way_compare(0., 0./0.); // 0<=>-nan : 000
116117
}
117118
\end{cppcode*}
118119
\end{exampleblock}
@@ -122,12 +123,12 @@
122123
\frametitlecpp[20]{Compiler-generated comparison operators}
123124
\begin{block}{For a given user-defined class}
124125
\begin{itemize}
125-
\item defining \mintinline{cpp}{operator<=>} allows the compiler to use it when encountering any other comparison operators
126-
\item of course, one can replace the default implementations
126+
\item defining \mintinline{cpp}{operator<=>} allows the compiler to use it when encountering the comparison operators \mintinline{cpp}{<}, \mintinline{cpp}{<=}, \mintinline{cpp}{>} and \mintinline{cpp}{>=}
127+
\item of course, one can still provide a custom implementation
127128
\item the compiler will \emph{NOT} add a default implementation for \mintinline{cpp}{operator==} and \mintinline{cpp}{operator!=}
128129
\begin{itemize}
129130
\item as those operators mean \textbf{equal}, rather than \textbf{equivalent}
130-
\item if \mintinline{cpp}{operator<=>} does not provide a strong order, it is advised not to define \mintinline{cpp}{operator==} yourself
131+
\item if \mintinline{cpp}{operator<=>} does not provide a strong order, it is advised not to define \mintinline{cpp}{operator==}
131132
\end{itemize}
132133
\end{itemize}
133134
\end{block}
@@ -153,12 +154,12 @@
153154
\frametitlecpp[20]{\texttt{operator<=>} summary}
154155
\begin{block}{Summary}
155156
\begin{itemize}
156-
\item Defining \mintinline{cpp}{operator<=>} allows you to use \mintinline{cpp}{operator<}, \mintinline{cpp}{operator>},\mintinline{cpp}{operator<=}, and \mintinline{cpp}{operator>=} for free
157+
\item Defining \mintinline{cpp}{operator<=>} allows you to use \mintinline{cpp}{operator<}, \mintinline{cpp}{operator>}, \mintinline{cpp}{operator<=}, and \mintinline{cpp}{operator>=} for free
157158
\item The standard library defines a few kinds of orderings
158159
\begin{itemize}
159160
\item strong, weak and partial
160161
\end{itemize}
161-
\item If \mintinline{cpp}{operator<=>} does not define a strong order, avoid defining \mintinline{cpp}{operator==}.
162+
\item If \mintinline{cpp}{operator<=>} does not define a strong ordering, avoid defining \mintinline{cpp}{operator==}
162163
\end{itemize}
163164
\end{block}
164165
\end{frame}

0 commit comments

Comments
 (0)