Skip to content

Commit 98f31a3

Browse files
bernhardmgrubersponce
authored andcommitted
Improve slides on perfect forwarding
1 parent 20b70cd commit 98f31a3

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

talk/expert/perfectforwarding.tex

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555
\end{cppcode*}
5656
\end{block}
5757
\begin{alertblock}{}
58-
\begin{itemize}
59-
\item \mintinline{cpp}{const T&} won't work when passing something non const
60-
\end{itemize}
58+
and \mintinline{cpp}{const T&} won't work when passing something non const
6159
\end{alertblock}
6260
\end{frame}
6361

@@ -78,7 +76,7 @@
7876
\end{frame}
7977

8078
\begin{frame}[fragile]
81-
\frametitlecpp[11]{The new problem: scaling to n arguments}
79+
\frametitlecpp[11]{The new problem: scaling to more arguments}
8280
\begin{block}{}
8381
\begin{cppcode*}{}
8482
template <typename T1, typename T2>
@@ -96,8 +94,10 @@
9694
\end{cppcode*}
9795
\end{block}{}
9896
\begin{alertblock}{Exploding complexity}
99-
3$^{n}$ complexity\\
100-
you do not want to try n = 5...
97+
\begin{itemize}
98+
\item for $n$ arguments, 3$^{n}$ overloads
99+
\item you do not want to try n = 5...
100+
\end{itemize}
101101
\end{alertblock}
102102
\end{frame}
103103

@@ -187,6 +187,22 @@
187187
};
188188
\end{cppcode*}
189189
\end{alertblock}
190+
\vspace{-1.2\baselineskip}
191+
\begin{overprint}
192+
\onslide<1>
193+
\begin{exampleblock}{Half-way correct version}
194+
\begin{cppcode*}{gobble=2}
195+
template <typename T> struct S {
196+
197+
template <typename U>
198+
S(U&& t) { ... } // deducing context -> fwd. ref.
199+
template <typename U>
200+
void f(U&& t) // deducing context -> fwd. ref.
201+
// ... but now U can be a different type than T
202+
};
203+
\end{cppcode*}
204+
\end{exampleblock}
205+
\onslide<2>
190206
\begin{exampleblock}{Correct version}
191207
\begin{cppcode*}{gobble=2}
192208
template <typename T> struct S {
@@ -199,6 +215,7 @@
199215
};
200216
\end{cppcode*}
201217
\end{exampleblock}
218+
\end{overprint}
202219
\end{frame}
203220

204221
\begin{frame}[fragile]
@@ -237,20 +254,20 @@
237254
\small
238255
\begin{cppcode*}{}
239256
template<typename T>
240-
T&& forward(remove_reference_t<T>& t) noexcept {
257+
T&& forward(remove_reference_t<T>& t) noexcept { // 1.
241258
return static_cast<T&&>(t);
242259
}
243260
template<typename T>
244-
T&& forward(remove_reference_t<T>&& t) noexcept {
261+
T&& forward(remove_reference_t<T>&& t) noexcept { // 2.
245262
return static_cast<T&&>(t);
246263
}
247264
\end{cppcode*}
248265
\end{block}
249266
\begin{block}{}
250267
\begin{itemize}
251-
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int}, it returns \mintinline{cpp}{int&&}
252-
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int&}, it returns \mintinline{cpp}{int& &&}, i.e.\ \mintinline{cpp}{int&}
253-
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int&&}, it returns \mintinline{cpp}{int&& &&}, i.e.\ \mintinline{cpp}{int&&}
268+
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int}, selects 2., returns \mintinline{cpp}{int&&}
269+
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int&}, selects 1., returns \mintinline{cpp}{int& &&}, i.e.\ \mintinline{cpp}{int&}
270+
\item if \mintinline{cpp}{T} is \mintinline{cpp}{int&&}, selects 2., returns \mintinline{cpp}{int&& &&}, i.e.\ \mintinline{cpp}{int&&}
254271
\end{itemize}
255272
\end{block}
256273
\end{frame}
@@ -269,17 +286,17 @@
269286
\begin{itemize}
270287
\item if we pass an rvalue reference \mintinline{cpp}{U&&} to wrapper
271288
\begin{itemize}
272-
\item arg will be of type \mintinline{cpp}{U&&}
289+
\item \mintinline{cpp}{T=U}, arg is of type \mintinline{cpp}{U&&}
273290
\item func will be called with a \mintinline{cpp}{U&&}
274291
\end{itemize}
275292
\item if we pass an lvalue reference \mintinline{cpp}{U&} to wrapper
276293
\begin{itemize}
277-
\item arg will be of type \mintinline{cpp}{U&}
294+
\item \mintinline{cpp}{T=U&}, arg is of type \mintinline{cpp}{U&} (reference collapsing)
278295
\item func will be called with a \mintinline{cpp}{U&}
279296
\end{itemize}
280297
\item if we pass a plain \mintinline{cpp}{U} (rvalue) to wrapper
281298
\begin{itemize}
282-
\item arg will be of type \mintinline{cpp}{U&&} (no copy in wrapper)
299+
\item \mintinline{cpp}{T=U}, arg is of type \mintinline{cpp}{U&&} (no copy in wrapper)
283300
\item func will be called with a \mintinline{cpp}{U&&}
284301
\item if func takes a plain \mintinline{cpp}{U}, copy happens there, as expected
285302
\end{itemize}

0 commit comments

Comments
 (0)