Skip to content

Commit d312100

Browse files
bernhardmgrubersponce
authored andcommitted
Improve basic slides
1 parent 92272bb commit d312100

File tree

6 files changed

+40
-32
lines changed

6 files changed

+40
-32
lines changed

talk/basicconcepts/arrayspointers.tex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@
7373
\begin{block}{A pointer to nothing}
7474
\begin{itemize}
7575
\item if a pointer doesn't point to anything, set it to \cppinline{nullptr}
76+
\begin{itemize}
77+
\item useful to e.g.\ mark the end of a linked data structure
78+
\item or absence of an optional function argument (pointer)
79+
\end{itemize}
7680
\item same as setting it to 0 or \cppinline{NULL} (before \cpp11)
7781
\item triggers compilation error when assigned to integer
7882
\end{itemize}
7983
\end{block}
8084
\pause
8185
\begin{exampleblock}{Example code}
8286
\begin{cppcode*}{}
83-
void* vp = nullptr;
8487
int* ip = nullptr;
85-
int i = NULL; // OK -> bug?
88+
int i = NULL; // compiles, bug?
8689
int i = nullptr; // ERROR
8790
\end{cppcode*}
8891
\end{exampleblock}

talk/basicconcepts/auto.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
\end{itemize}
1111
\begin{cppcode*}{}
1212
std::vector<int> v;
13-
int a = v[3];
13+
float a = v[3]; // conversion intended?
1414
int b = v.size(); // bug? unsigned to signed
1515
\end{cppcode*}
1616
\end{block}
@@ -19,7 +19,7 @@
1919
\begin{cppcode*}{}
2020
std::vector<int> v;
2121
auto a = v[3];
22-
const auto b = v.size();
22+
const auto b = v.size(); // std::size_t
2323
int sum{0};
2424
for (auto n : v) { sum += n; }
2525
\end{cppcode*}

talk/basicconcepts/classenum.tex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@
8686
\vfill \null
8787
\end{multicols}
8888
\onslide<7->{
89-
\begin{exampleblock}{}
90-
Starting with \cpp17: prefer \cppinline{std::variant}
91-
\end{exampleblock}
89+
\begin{goodpractice}{Avoid unions}
90+
\begin{itemize}
91+
\item Starting with \cpp17: prefer \cppinline{std::variant}
92+
\end{itemize}
93+
\end{goodpractice}
9294
}
9395
\end{frame}
9496

talk/basicconcepts/control.tex

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@
5656
\pause
5757
\begin{exampleblock}{Practical example}
5858
\begin{cppcode*}{}
59-
int collatz(int a) {
60-
return a==1 ? 1 : collatz(a%2==0 ? a/2 : 3*a+1);
61-
}
59+
const int charge = isLepton ? -1 : 0;
6260
\end{cppcode*}
6361
\end{exampleblock}
6462
\pause
6563
\begin{alertblock}{Do not abuse it}
64+
\begin{cppcode*}{}
65+
int collatz(int a) {
66+
return a==1 ? 1 : collatz(a%2==0 ? a/2 : 3*a+1);
67+
}
68+
\end{cppcode*}
6669
\begin{itemize}
6770
\item Explicit \cppinline{if}s are generally easier to read
6871
\item Use the ternary operator with short conditions and expressions
@@ -103,17 +106,17 @@
103106
enum class Lang { French, German, English, Other };
104107
Lang language = ...;
105108
switch (language) {
106-
case Lang::French:
107-
std::cout << "Bonjour";
108-
break;
109-
case Lang::German:
110-
std::cout << "Guten Tag";
111-
break;
112-
case Lang::English:
113-
std::cout << "Good morning";
114-
break;
115-
default:
116-
std::cout << "I do not speak your language";
109+
case Lang::French:
110+
std::cout << "Bonjour";
111+
break;
112+
case Lang::German:
113+
std::cout << "Guten Tag";
114+
break;
115+
case Lang::English:
116+
std::cout << "Good morning";
117+
break;
118+
default:
119+
std::cout << "I do not speak your language";
117120
}
118121
\end{cppcode*}
119122
\end{exampleblock}
@@ -144,17 +147,16 @@
144147

145148
\begin{frame}[fragile]
146149
\frametitlecpp[17]{Init-statements for if and switch}
147-
\begin{block}{}
150+
\begin{block}{Purpose}
148151
Allows to limit variable scope in \cppinline{if} and \cppinline{switch} statements
149152
\end{block}
150153
\begin{exampleblock}{\cpp17}
151154
\begin{cppcode*}{}
152155
if (Value val = GetValue(); condition(val)) {
153-
f(val);
154-
} else {
155-
g(val);
156-
}
157-
h(val); // compile error
156+
f(val); // ok
157+
} else
158+
g(val); // ok
159+
h(val); // error, no `val` in scope here
158160
\end{cppcode*}
159161
\end{exampleblock}
160162
\pause
@@ -224,7 +226,7 @@
224226

225227
\begin{frame}[fragile]
226228
\frametitlecpp[20]{Init-statements for range-based loops}
227-
\begin{block}{}
229+
\begin{block}{Purpose}
228230
Allows to limit variable scope in range-based loops
229231
\end{block}
230232
\begin{alertblock}{\cpp17}
@@ -252,6 +254,7 @@
252254
while(condition) {
253255
statements;
254256
}
257+
255258
do {
256259
statements;
257260
} while(condition);

talk/basicconcepts/operators.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
\begin{frame}[fragile]
44
\frametitlecpp[98]{Operators(1)}
5-
\begin{block}{Binary \& Assignment Operators}
5+
\begin{block}{Binary and Assignment Operators}
66
\begin{cppcode*}{}
77
int i = 1 + 4 - 2; // 3
88
i *= 3; // 9, short for: i = i * 3;
@@ -11,7 +11,7 @@
1111
\end{cppcode*}
1212
\end{block}
1313
\pause
14-
\begin{block}{Increment / Decrement \uncover<3->{\hfill \alert{\bf Use wisely}}}
14+
\begin{block}{Increment / Decrement Operators \uncover<3->{\hfill \alert{\bf Use wisely}}}
1515
\begin{cppcode*}{}
1616
int i = 0; i++; // i = 1
1717
int j = ++i; // i = 2, j = 2
@@ -35,7 +35,7 @@
3535
\end{cppcode*}
3636
\end{block}
3737
\pause
38-
\begin{block}{Boolean Operators}
38+
\begin{block}{Logical Operators}
3939
\begin{cppcode*}{}
4040
bool a = true;
4141
bool b = false;

talk/basicconcepts/references.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
\item References allow for direct access to another object
88
\item They can be used as shortcuts / better readability
99
\item They can be declared \cppinline{const} to allow only read access
10-
\item They can be used as function arguments
1110
\end{itemize}
1211
\end{block}
1312

@@ -21,6 +20,7 @@
2120
struct A { int x; int y; } a;
2221
const int &x = a.x; // direct read access to A's x
2322
x = 4; // doesn't compile
23+
a.x = 4; // fine
2424
\end{cppcode*}
2525
\end{exampleblock}
2626
\end{frame}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy