|
169 | 169 | \end{exampleblock}
|
170 | 170 | \end{frame}
|
171 | 171 |
|
| 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 | + |
172 | 202 | \begin{frame}[fragile]
|
173 | 203 | \frametitlecpp[11]{Anatomy of a lambda}
|
174 | 204 | \begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}}
|
|
0 commit comments