Skip to content

Commit 8f9cbfb

Browse files
committed
Extend lambda slides with init captures and comparison of val vs ref.
1 parent 3485551 commit 8f9cbfb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

talk/morelanguage/lambda.tex

+30
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@
169169
\end{exampleblock}
170170
\end{frame}
171171

172+
\begin{frame}[fragile]
173+
\frametitlecpp[11]{Capture by value vs.\ by reference}
174+
\begin{exampleblock}{See the difference between val and ref}
175+
\begin{cppcode*}{}
176+
int data[]{1,9,3,8,3,7,4,6,5};
177+
int increment = 3;
178+
auto val = [ inc](int x) { return x+inc; };
179+
auto ref = [&inc](int x) { return x+inc; };
180+
181+
increment = 4;
182+
183+
for(int& i : data) i = val(i); // increments by 3
184+
for(int& i : data) i = ref(i); // increments by 4
185+
\end{cppcode*}
186+
\end{exampleblock}
187+
\end{frame}
188+
189+
\begin{frame}[fragile]
190+
\frametitlecpp[14]{Init capture}
191+
\begin{exampleblock}{Capture with an initializer}
192+
In \cpp14, can declare captures with initializers
193+
\begin{cppcode*}{}
194+
auto f = [inc = 1+2](int x) { return x+inc; };
195+
auto g = [inc = getInc()](int x) { return x+inc; };
196+
for(int& i : data) i = f(i); // increments by 3
197+
for(int& i : data) i = g(i); // unknown increment
198+
\end{cppcode*}
199+
\end{exampleblock}
200+
\end{frame}
201+
172202
\begin{frame}[fragile]
173203
\frametitlecpp[11]{Anatomy of a lambda}
174204
\begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}}

0 commit comments

Comments
 (0)