|
4 | 4 | \frametitlecpp[11]{Trailing function return type}
|
5 | 5 | \begin{block}{An alternate way to specify a function's return type}
|
6 | 6 | \begin{cppcode*}{linenos=false}
|
7 |
| - ReturnType func(Arg1 a, Arg2 b); // classic |
8 |
| - auto func(Arg1 a, Arg2 b) -> ReturnType; |
| 7 | + int f(float a); // classic |
| 8 | + auto f(float a) -> int; // trailing |
| 9 | + auto f(float a) { return 42; } // deduced, C++14 |
9 | 10 | \end{cppcode*}
|
10 | 11 | \end{block}
|
11 | 12 | \pause
|
12 | 13 | \begin{block}{Advantages}
|
13 | 14 | \begin{itemize}
|
14 | 15 | \item Allows to simplify inner type definition
|
15 | 16 | \begin{cppcode*}{gobble=4}
|
16 |
| - class Class { |
17 |
| - using ReturnType = int; |
18 |
| - ReturnType func(); |
| 17 | + class Equation { |
| 18 | + using ResultType = double; |
| 19 | + ResultType evaluate(); |
19 | 20 | }
|
20 |
| - Class::ReturnType Class::func() {...} |
21 |
| - auto Class::func() -> ReturnType {...} |
| 21 | + Equation::ResultType Equation::evaluate() {...} |
| 22 | + auto Equation::evaluate() -> ResultType {...} |
22 | 23 | \end{cppcode*}
|
23 |
| - \item \cpp14: \cppinline{ReturnType} not required, compiler can deduce it |
24 |
| - \item used by lambda expressions |
| 24 | + \item Used by lambda expressions |
25 | 25 | \end{itemize}
|
26 | 26 | \end{block}
|
27 | 27 | \end{frame}
|
|
30 | 30 | \begin{frame}[fragile]
|
31 | 31 | \frametitlecpp[11]{Lambda expressions}
|
32 | 32 | \begin{block}{Definition}
|
33 |
| - a lambda expression is a function with no name |
| 33 | + A lambda expression is a function with no name |
34 | 34 | \end{block}
|
35 | 35 | \pause
|
36 | 36 | \begin{exampleblock}{Python example}
|
|
141 | 141 | \end{alertblock}
|
142 | 142 | \pause
|
143 | 143 | \begin{block}{Explanation}
|
144 |
| - By default, variables are captured by value, and the lambda's \cppinline{operator()} is \cppinline{const}. |
| 144 | + \begin{itemize} |
| 145 | + \item By default, variables are captured by value |
| 146 | + \item The lambda's \cppinline{operator()} is \cppinline{const} |
| 147 | + \end{itemize} |
145 | 148 | \end{block}
|
146 | 149 | \end{frame}
|
147 | 150 |
|
|
160 | 163 | \begin{exampleblock}{Mixed case}
|
161 | 164 | One can of course mix values and references
|
162 | 165 | \begin{cppcode*}{firstnumber=5}
|
163 |
| - int sum = 0, offset = 1; |
| 166 | + int sum = 0, off = 1; |
164 | 167 | int data[]{1,9,3,8,3,7,4,6,5};
|
165 |
| - auto f = [&sum, offset](int x) { sum += x+offset; }; |
| 168 | + auto f = [&sum, off](int x) { sum += x + off; }; |
166 | 169 | for (int i : data) f(i);
|
167 | 170 | \end{cppcode*}
|
168 | 171 | \end{exampleblock}
|
169 | 172 | \end{frame}
|
170 | 173 |
|
| 174 | +\begin{frame}[fragile] |
| 175 | + \frametitlecpp[11]{Anatomy of a lambda} |
| 176 | + \begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}} |
| 177 | + \begin{itemize} |
| 178 | + \item They are replaced by a functor during compilation |
| 179 | + \end{itemize} |
| 180 | + \begin{columns} |
| 181 | + \scriptsize |
| 182 | + \begin{column}{.25\textwidth} |
| 183 | + \begin{cppcode*}{gobble=6} |
| 184 | + int sum = 0, off = 1; |
| 185 | + auto l = |
| 186 | + [&sum, off] |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + (int x) { |
| 191 | + sum += x + off; |
| 192 | + }; |
| 193 | + |
| 194 | + |
| 195 | + l(42); |
| 196 | + \end{cppcode*} |
| 197 | + \end{column} |
| 198 | + \begin{column}{.45\textwidth} |
| 199 | + \begin{cppcode*}{gobble=6, firstnumber=13} |
| 200 | + int sum = 0, off = 1; |
| 201 | + struct __lambda4 { |
| 202 | + int& sum; int off; |
| 203 | + __lambda4(int& s, int o) |
| 204 | + : sum(s), off(o) {} |
| 205 | + |
| 206 | + auto operator()(int x) const { |
| 207 | + sum += x + off; |
| 208 | + } |
| 209 | + }; |
| 210 | + auto l = __lambda4{sum, off}; |
| 211 | + l(42); |
| 212 | + \end{cppcode*} |
| 213 | + \end{column} |
| 214 | + \end{columns} |
| 215 | + \end{block} |
| 216 | + \begin{exampleblock}{Some nice consequence} |
| 217 | + \begin{itemize} |
| 218 | + \item Lambda expressions create ordinary objects |
| 219 | + \item They can be copied, moved, or inherited from |
| 220 | + \end{itemize} |
| 221 | + \end{exampleblock} |
| 222 | +\end{frame} |
| 223 | + |
171 | 224 | \begin{frame}[fragile]
|
172 | 225 | \frametitlecpp[11]{Capture list}
|
173 | 226 | \begin{block}{all by value}
|
|
219 | 272 | Details in \href{https://www.nextptr.com/tutorial/ta1430524603/capture-this-in-lambda-expression-timeline-of-change}{this blog post}.
|
220 | 273 | \end{frame}
|
221 | 274 |
|
222 |
| -\begin{frame}[fragile] |
223 |
| - \frametitlecpp[11]{Anatomy of a lambda} |
224 |
| - \begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}} |
225 |
| - \begin{itemize} |
226 |
| - \item they are replaced by a functor during compilation |
227 |
| - \end{itemize} |
228 |
| - \begin{columns} |
229 |
| - \scriptsize |
230 |
| - \begin{column}{.25\textwidth} |
231 |
| - \begin{cppcode*}{gobble=6} |
232 |
| - int sum = 0, off = 1; |
233 |
| - auto l = |
234 |
| - [&sum, off] |
235 |
| - |
236 |
| - |
237 |
| - |
238 |
| - (int x) { |
239 |
| - sum += x + off; |
240 |
| - }; |
241 |
| - |
242 |
| - |
243 |
| - l(42); |
244 |
| - \end{cppcode*} |
245 |
| - \end{column} |
246 |
| - \begin{column}{.45\textwidth} |
247 |
| - \begin{cppcode*}{gobble=6, firstnumber=13} |
248 |
| - int sum = 0, off = 1; |
249 |
| - struct __lambda4 { |
250 |
| - int& sum; |
251 |
| - int off; |
252 |
| - __lambda4(int& s, int o) |
253 |
| - : sum(s), off(o) {} |
254 |
| - auto operator()(int x)const{ |
255 |
| - sum += x + off; |
256 |
| - } |
257 |
| - }; |
258 |
| - auto l = __lambda4{sum, off}; |
259 |
| - l(42); |
260 |
| - \end{cppcode*} |
261 |
| - \end{column} |
262 |
| - \end{columns} |
263 |
| - \end{block} |
264 |
| - \begin{exampleblock}{Some nice consequence} |
265 |
| - \begin{itemize} |
266 |
| - \item lambda expressions create ordinary objects |
267 |
| - \item they can in particular be inherited from! |
268 |
| - \end{itemize} |
269 |
| - \end{exampleblock} |
270 |
| -\end{frame} |
271 |
| - |
272 | 275 | \begin{frame}[fragile]
|
273 | 276 | \frametitlecpp[14]{Generic lambdas}
|
274 | 277 | \begin{block}{Generic lambdas (aka.\ polymorphic lambdas)}
|
|
0 commit comments