Skip to content

Commit 7147d29

Browse files
committed
add lambda with argument example
1 parent dde6776 commit 7147d29

14 files changed

Lines changed: 177 additions & 8 deletions

‎slides.pdf‎

10.4 KB
Binary file not shown.

‎slides/part1.tex‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,34 @@ \section{Syntax}
2222
A slightly more \enquote{useful} lambda
2323
\inputcpplisting{snippet3}
2424
\end{column}
25-
\begin{column}{.45\textwidth}
26-
\ldots is equivalent to
25+
\begin{column}{.46\textwidth}
26+
\ldots is equivalent to$\textcolor{vertexDarkRed}{{}^\star}$
2727
\inputcpplisting{snippet4}
2828
\end{column}
2929
\end{columns}
30+
31+
\vspace{5mm}
32+
33+
\footnotesize $\textcolor{vertexDarkRed}{{}^\star}$ check Compiler Explorer links (note $\texttt{-O0}$\,!)
34+
\end{frame}
35+
36+
\begin{frame}[fragile]{\texttt{C++}'s Lambda Expression}
37+
\begin{columns}[t]
38+
\begin{column}{.45\textwidth}
39+
An even more \enquote{useful} lambda
40+
\inputcpplisting{snippet3b}
41+
\end{column}
42+
\begin{column}{.46\textwidth}
43+
\ldots is equivalent to$\textcolor{vertexDarkRed}{{}^\star}$
44+
\inputcpplisting{snippet4b}
45+
\end{column}
46+
\end{columns}
47+
48+
Lambda expressions are \textbf{pure syntactic sugar} and are equivalent to \texttt{struct}s with an appropriate \texttt{operator()()} overload!
49+
50+
\vspace{5mm}
51+
52+
\footnotesize $\textcolor{vertexDarkRed}{{}^\star}$ check Compiler Explorer links (note $\texttt{-O0}$\,!)
3053
\end{frame}
3154

3255
\begin{frame}[fragile]{Q: What is the output of the program?}

‎slides/part1_pv.tex‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,34 @@ \section{Syntax}
2222
A slightly more \enquote{useful} lambda
2323
\inputcpplisting{snippet3}
2424
\end{column}
25-
\begin{column}{.45\textwidth}
26-
\ldots is equivalent to
25+
\begin{column}{.46\textwidth}
26+
\ldots is equivalent to$\textcolor{vertexDarkRed}{{}^\star}$
2727
\inputcpplisting{snippet4}
2828
\end{column}
2929
\end{columns}
30+
31+
\vspace{5mm}
32+
33+
\footnotesize $\textcolor{vertexDarkRed}{{}^\star}$ check Compiler Explorer links (note $\texttt{-O0}$\,!)
34+
\end{frame}
35+
36+
\begin{frame}[fragile]{\texttt{C++}'s Lambda Expression}
37+
\begin{columns}[t]
38+
\begin{column}{.45\textwidth}
39+
An even more \enquote{useful} lambda
40+
\inputcpplisting{snippet3b}
41+
\end{column}
42+
\begin{column}{.46\textwidth}
43+
\ldots is equivalent to$\textcolor{vertexDarkRed}{{}^\star}$
44+
\inputcpplisting{snippet4b}
45+
\end{column}
46+
\end{columns}
47+
48+
Lambda expressions are \textbf{pure syntactic sugar} and are equivalent to \texttt{struct}s with an appropriate \texttt{operator()()} overload!
49+
50+
\vspace{5mm}
51+
52+
\footnotesize $\textcolor{vertexDarkRed}{{}^\star}$ check Compiler Explorer links (note $\texttt{-O0}$\,!)
3053
\end{frame}
3154

3255
\begin{frame}[fragile]{Q: What is the output of the program?}

‎snippets/snippet37_cpplst.tex‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
\begin{lstlisting}[title=\href{https://godbolt.org/z/YY44v7}{\texttt{godbolt.org/z/YY44v7}}]
1+
\begin{lstlisting}[title=\href{https://godbolt.org/z/cT7E9j}{\texttt{godbolt.org/z/cT7E9j}}]
22
#include <iostream>
33
#include <optional>
44

@@ -7,9 +7,8 @@
77
auto o = []() -> opt_t {
88
return 42;
99
}();
10-
std::cout << (*o)++;
11-
1210
const auto co = o;
13-
(*co)++; // error!
11+
std::cout << (*o)++; // O.K.
12+
std::cout << (*co)++; // error!
1413
}
1514
\end{lstlisting}

‎snippets/snippet3b.asm‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# g92 -O0
2+
| main::{lambda(int)#1}::operator()(int) const:
3+
2| push rbp
4+
2| mov rbp, rsp
5+
2| mov QWORD PTR [rbp-8], rdi
6+
2| mov DWORD PTR [rbp-12], esi
7+
2| mov eax, DWORD PTR [rbp-12]
8+
2| pop rbp
9+
2| ret
10+
| main:
11+
1| push rbp
12+
1| mov rbp, rsp
13+
1| sub rsp, 16
14+
3| lea rax, [rbp-1]
15+
3| mov esi, 42
16+
3| mov rdi, rax
17+
3| call main::{lambda(int)#1}::operator()(int) const
18+
3| nop
19+
4| leave
20+
4| ret

‎snippets/snippet3b.cfg‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cpp_file": "snippet3b.cpp",
3+
"compiler": "g92",
4+
"flags": "-O0",
5+
"compile": true
6+
}

‎snippets/snippet3b.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
auto x = [](int y) { return y; };
3+
return x(42);
4+
}

‎snippets/snippet3b_asmlst.tex‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
\begin{lstlisting}[language={},numbers=none,title=\href{https://godbolt.org/z/963TMh}{\texttt{godbolt.org/z/963TMh}}]
2+
# g92 -O0
3+
| main::{lambda(int)#1}::operator()(int) const:
4+
2| push rbp
5+
2| mov rbp, rsp
6+
2| mov QWORD PTR [rbp-8], rdi
7+
2| mov DWORD PTR [rbp-12], esi
8+
2| mov eax, DWORD PTR [rbp-12]
9+
2| pop rbp
10+
2| ret
11+
| main:
12+
1| push rbp
13+
1| mov rbp, rsp
14+
1| sub rsp, 16
15+
3| lea rax, [rbp-1]
16+
3| mov esi, 42
17+
3| mov rdi, rax
18+
3| call main::{lambda(int)#1}::operator()(int) const
19+
3| nop
20+
4| leave
21+
4| ret
22+
\end{lstlisting}

‎snippets/snippet3b_cpplst.tex‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\begin{lstlisting}[title=\href{https://godbolt.org/z/963TMh}{\texttt{godbolt.org/z/963TMh}}]
2+
int main() {
3+
auto x = [](int y) { return y; };
4+
return x(42);
5+
}
6+
\end{lstlisting}

‎snippets/snippet4b.asm‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# g92 -O0
2+
| main::{unnamed type#1}::operator()(int) const:
3+
3| push rbp
4+
3| mov rbp, rsp
5+
3| mov QWORD PTR [rbp-8], rdi
6+
3| mov DWORD PTR [rbp-12], esi
7+
4| mov eax, DWORD PTR [rbp-12]
8+
5| pop rbp
9+
5| ret
10+
| main:
11+
1| push rbp
12+
1| mov rbp, rsp
13+
1| sub rsp, 16
14+
7| lea rax, [rbp-1]
15+
7| mov esi, 42
16+
7| mov rdi, rax
17+
7| call main::{unnamed type#1}::operator()(int) const
18+
7| nop
19+
8| leave
20+
8| ret

0 commit comments

Comments
 (0)