Skip to content

Commit 4dfec0b

Browse files
Small improvements for more language chapter (#451)
Those are inspired by my thoughts during the Friday essentials course lecture.
1 parent 1018b25 commit 4dfec0b

File tree

4 files changed

+115
-79
lines changed

4 files changed

+115
-79
lines changed

Diff for: talk/morelanguage/constness.tex

+11-6
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@
6262
// type of 'this' is 'Example const*'
6363
data = 0; // Error: member function is const
6464
}
65+
void foo() { // ok, overload
66+
data = 1; // ok, 'this' is 'Example*'
67+
}
6568
int data;
6669
};
70+
Example const e1; e1.foo(); // calls const foo
71+
Example e2; e2.foo(); // calls non-const foo
6772
\end{cppcode}
6873
\end{frame}
6974

@@ -76,16 +81,16 @@
7681
\end{itemize}
7782
\end{block}
7883
\begin{cppcode}
79-
void func(int & a);
80-
void funcConst(int const & a);
84+
void change(int & a);
85+
void read(int const & a);
8186

8287
int a = 0;
8388
int const b = 0;
8489

85-
func(a); // ok
86-
func(b); // error
87-
funcConst(a); // ok
88-
funcConst(b); // ok
90+
change(a); // ok
91+
change(b); // error
92+
read(a); // ok
93+
read(b); // ok
8994
\end{cppcode}
9095
\end{frame}
9196

Diff for: talk/morelanguage/lambda.tex

+66-63
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
\frametitlecpp[11]{Trailing function return type}
55
\begin{block}{An alternate way to specify a function's return type}
66
\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
910
\end{cppcode*}
1011
\end{block}
1112
\pause
1213
\begin{block}{Advantages}
1314
\begin{itemize}
1415
\item Allows to simplify inner type definition
1516
\begin{cppcode*}{gobble=4}
16-
class Class {
17-
using ReturnType = int;
18-
ReturnType func();
17+
class Equation {
18+
using ResultType = double;
19+
ResultType evaluate();
1920
}
20-
Class::ReturnType Class::func() {...}
21-
auto Class::func() -> ReturnType {...}
21+
Equation::ResultType Equation::evaluate() {...}
22+
auto Equation::evaluate() -> ResultType {...}
2223
\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
2525
\end{itemize}
2626
\end{block}
2727
\end{frame}
@@ -30,7 +30,7 @@
3030
\begin{frame}[fragile]
3131
\frametitlecpp[11]{Lambda expressions}
3232
\begin{block}{Definition}
33-
a lambda expression is a function with no name
33+
A lambda expression is a function with no name
3434
\end{block}
3535
\pause
3636
\begin{exampleblock}{Python example}
@@ -141,7 +141,10 @@
141141
\end{alertblock}
142142
\pause
143143
\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}
145148
\end{block}
146149
\end{frame}
147150

@@ -160,14 +163,64 @@
160163
\begin{exampleblock}{Mixed case}
161164
One can of course mix values and references
162165
\begin{cppcode*}{firstnumber=5}
163-
int sum = 0, offset = 1;
166+
int sum = 0, off = 1;
164167
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; };
166169
for (int i : data) f(i);
167170
\end{cppcode*}
168171
\end{exampleblock}
169172
\end{frame}
170173

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+
171224
\begin{frame}[fragile]
172225
\frametitlecpp[11]{Capture list}
173226
\begin{block}{all by value}
@@ -219,56 +272,6 @@
219272
Details in \href{https://www.nextptr.com/tutorial/ta1430524603/capture-this-in-lambda-expression-timeline-of-change}{this blog post}.
220273
\end{frame}
221274

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-
272275
\begin{frame}[fragile]
273276
\frametitlecpp[14]{Generic lambdas}
274277
\begin{block}{Generic lambdas (aka.\ polymorphic lambdas)}

Diff for: talk/morelanguage/stl.tex

+9-10
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@
3333
#include <iterator>
3434
#include <iostream>
3535

36-
std::vector<int> in{5, 3, 4}; // initializer list
37-
std::vector<int> out(3); // constructor taking size
38-
std::transform(in.begin(), in.end(), // range1
39-
in.begin(), // start range2
40-
out.begin(), // start result
41-
std::multiplies{}); // function obj
42-
std::copy(out.begin(), out.end(), // 25 9 16
43-
std::ostream_iterator<int>{std::cout, " "});
36+
std::vector<int> in{5, 3, 4}; // initializer list
37+
std::vector<int> out(3); // constructor taking size
38+
std::transform(in.begin(), in.end(), // input range
39+
out.begin(), // start result
40+
std::negate{}); // function obj
41+
std::copy(out.begin(), out.end(), // -5 -3 -4
42+
std::ostream_iterator<int>{std::cout, " "});
4443
\end{cppcode*}
4544
\end{exampleblockGB}
4645
\end{frame}
@@ -190,7 +189,7 @@
190189
\end{block}
191190
\begin{exampleblockGB}{Iterator example}{https://godbolt.org/z/jv1qTo5xz}{Iterator example}
192191
\begin{cppcode*}{}
193-
std::vector<int> const v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
192+
std::vector<int> const v = {1,2,3,4,5,6,7,8,9};
194193
auto const end = v.rend() - 3; // arithmetic
195194
for (auto it = v.rbegin();
196195
it != end; // compare positions
@@ -337,7 +336,7 @@
337336
// Computes sin(x)/(x + DBL_MIN) for elements of a range.
338337
std::vector<double> r(l.size());
339338
std::transform(l.begin(), l.end(), r.begin(),
340-
[](auto x) { return sin(x)/(x + DBL_MIN); });
339+
[](auto x) { return std::sin(x)/(x + DBL_MIN); });
341340

342341
// reduce/fold (using addition)
343342
const auto sum = std::reduce(v.begin(), v.end());

Diff for: talk/morelanguage/templates.tex

+29
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@
9696
struct Map {
9797
void set(const KeyType &key, ValueType value);
9898
ValueType get(const KeyType &key);
99+
...
99100
};
100101

101102
Map<std::string, int> m1;
102103
Map<float> m2; // Map<float, float>
103104
Map<> m3; // Map<int, int>
105+
Map m4; // Map<int, int>, C++17
104106
\end{cppcode*}
105107
\end{frame}
106108

@@ -151,6 +153,33 @@
151153
\end{cppcode*}
152154
\end{frame}
153155

156+
\begin{advanced}
157+
158+
\begin{frame}[fragile]
159+
\frametitlecpp[98]{Nested templates}
160+
\begin{exampleblockGB}{Nested templates}{https://godbolt.org/z/a9nPnK9jx}{Nested templates}
161+
\small
162+
\begin{cppcode*}{gobble=8}
163+
template<typename KeyType=int, typename ValueType=KeyType>
164+
struct Map {
165+
template<typename OtherValueType>
166+
void set(const KeyType &key, OtherValueType value) {
167+
...
168+
}
169+
template<typename OtherKeyType>
170+
ValueType get(const OtherKeyType &key);
171+
};
172+
173+
template<typename KeyType, typename ValueType> //for class
174+
template<typename OtherKeyType> //for member function
175+
ValueType Map<KeyType, ValueType>::get
176+
(const OtherKeyType &key) { ... }
177+
\end{cppcode*}
178+
\end{exampleblockGB}
179+
\end{frame}
180+
181+
\end{advanced}
182+
154183
\begin{frame}[fragile]
155184
\frametitle{Non-type template parameter \hfill \cpp98 / \cpp17 / \cpp20}
156185
\begin{block}{template parameters can also be values}

0 commit comments

Comments
 (0)