Skip to content

Commit 840c061

Browse files
Small improvements for more language chapter
Co-authored-by: Sebastien Ponce <sebastien.ponce_github@m4x.org>
1 parent fecc3a9 commit 840c061

File tree

6 files changed

+187
-161
lines changed

6 files changed

+187
-161
lines changed

talk/morelanguage/constness.tex

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,44 @@
44
\frametitlecpp[98]{Constness}
55
\begin{block}{The \texttt{const} keyword}
66
\begin{itemize}
7-
\item indicate that the element to the left is constant
7+
\item indicates that the element to the left is constant
8+
\begin{itemize}
9+
\item when nothing on the left, applies to the right
10+
\end{itemize}
811
\item this element won't be modifiable in the future
912
\item this is all checked at compile time
1013
\end{itemize}
1114
\end{block}
1215
\begin{cppcode}
13-
// standard syntax
1416
int const i = 6;
17+
const int i = 6; // equivalent
1518

1619
// error : i is constant
1720
i = 5;
1821

19-
// also ok, when nothing on the left,
20-
// const applies to the element on the right
21-
const int j = 6;
22+
auto const j = i; // works with auto
2223
\end{cppcode}
2324
\end{frame}
2425

2526
\begin{frame}[fragile]
2627
\frametitlecpp[98]{Constness and pointers}
27-
\scriptsize
28+
\small
2829
\begin{cppcode}
29-
// pointer to a constant integer
3030
int a = 1, b = 2;
31-
int const *i = &a;
31+
32+
int const *i = &a; // pointer to const int
3233
*i = 5; // error, int is const
3334
i = &b; // ok, pointer is not const
3435

35-
// constant pointer to an integer
36-
int * const j = &a;
36+
int * const j = &a; // const pointer to int
3737
*j = 5; // ok, value can be changed
3838
j = &b; // error, pointer is const
3939

40-
// constant pointer to a constant integer
41-
int const * const k = &a;
40+
int const * const k = &a; // const pointer to const int
4241
*k = 5; // error, value is const
4342
k = &b; // error, pointer is const
4443

45-
// const reference
46-
int const & l = a;
44+
int const & l = a; // reference to const int
4745
l = b; // error, reference is const
4846

4947
int const & const l = a; // compile error
@@ -54,17 +52,17 @@
5452
\frametitlecpp[98]{Member function constness}
5553
\begin{block}{The \texttt{const} keyword for member functions}
5654
\begin{itemize}
57-
\item indicate that the function does not modify the object
55+
\item indicates that the function does not modify the object
5856
\item in other words, \cppinline{this} is a pointer to a constant object
5957
\end{itemize}
6058
\end{block}
6159
\begin{cppcode}
6260
struct Example {
6361
void foo() const {
6462
// type of 'this' is 'Example const*'
65-
m_member = 0; // Error: member function is const
63+
data = 0; // Error: member function is const
6664
}
67-
int m_member;
65+
int data;
6866
};
6967
\end{cppcode}
7068
\end{frame}
@@ -74,7 +72,7 @@
7472
\begin{block}{Constness is part of the type}
7573
\begin{itemize}
7674
\item \cppinline{T const} and \cppinline{T} are different types
77-
\item however, \cppinline{T} is automatically cast to \cppinline{T const} when needed
75+
\item however: \cppinline{T} is automatically cast to \cppinline{T const} when needed
7876
\end{itemize}
7977
\end{block}
8078
\begin{cppcode}

talk/morelanguage/exceptions.tex

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

33
\begin{frame}[fragile]
44
\frametitlecpp[98]{Exceptions}
5-
\begin{block}{The concept}
5+
\begin{block}{Purpose}
66
\begin{itemize}
77
\item to handle \textit{exceptional} events that happen rarely
88
\item and cleanly jump to a place where the error can be handled
@@ -30,7 +30,7 @@
3030
}
3131
\end{cppcode*}
3232
\columnbreak
33-
\begin{cppcode*}{fontsize=\small,gobble=2}
33+
\begin{cppcode*}{fontsize=\small,firstnumber=7,gobble=2}
3434
void process_data(file &f) {
3535
...
3636
if (i >= buffer.size())
@@ -46,32 +46,61 @@
4646
\begin{block}{Throwing exceptions}
4747
\begin{itemize}
4848
\item objects of any type can be thrown (even e.g.\ \cppinline{int})
49-
\begin{itemize}
50-
\item prefer standard exception classes
51-
\end{itemize}
52-
\item throw objects by value
5349
\end{itemize}
5450
\end{block}
51+
\begin{goodpractice}{Throwing exceptions}
52+
\begin{itemize}
53+
\item prefer throwing standard exception classes
54+
\item throw objects by value
55+
\end{itemize}
56+
\end{goodpractice}
57+
\small
5558
\begin{cppcode}
59+
#include <stdexcept>
5660
void process_data(file& f) {
5761
if (!f.open())
5862
throw std::invalid_argument{"stream is not open"};
59-
6063
auto header = read_line(f); // may throw an IO error
6164
if (!header.starts_with("BEGIN"))
6265
throw std::runtime_error{"invalid file content"};
63-
6466
std::string body(f.size()); // may throw std::bad_alloc
6567
...
6668
}
6769
\end{cppcode}
6870
\end{frame}
6971

72+
\begin{frame}[fragile]
73+
\frametitlecpp[98]{Exceptions}
74+
\begin{block}{Standard exceptions}
75+
\begin{itemize}
76+
\item \cppinline{std::exception}, defined in header \cppinline{<exception>}
77+
\begin{itemize}
78+
\item Base class of all standard exceptions
79+
\item Get error message: \cppinline{virtual const char* what() const;}
80+
\item Please derive your own exception classes from this one
81+
\end{itemize}
82+
\item From \cppinline{<stdexcept>}:
83+
\begin{itemize}
84+
\item \cppinline{std::runtime_error}, \cppinline{std::logic_error}, \cppinline{std::out_of_range}, \cppinline{std::invalid_argument}, ...
85+
\item Store a string: \cppinline{throw std::runtime_error{"msg"}}
86+
\item You should use these the most
87+
\end{itemize}
88+
\item \cppinline{std::bad_alloc}, defined in header \cppinline{<new>}
89+
\begin{itemize}
90+
\item Thrown by standard allocation functions (e.g.\ \cppinline{new})
91+
\item Signals failure to allocate
92+
\item Carries no message
93+
\end{itemize}
94+
\item ...
95+
\end{itemize}
96+
\end{block}
97+
\end{frame}
98+
7099
\begin{frame}[fragile]
71100
\frametitlecpp[98]{Exceptions}
72101
\begin{block}{Catching exceptions}
73102
\begin{itemize}
74-
\item a catch clause catches an exception of the same or derived type
103+
\item a catch clause catches exceptions of the same or derived type
75104
\item multiple catch clauses will be matched in order
76105
\item if no catch clause matches, the exception propagates
77106
\item if the exception is never caught, \cppinline{std::terminate} is called
@@ -86,13 +115,18 @@
86115
std::cerr << "Failed to process file: " << e.what();
87116
}
88117
\end{cppcode}
118+
\begin{goodpractice}{Catching exceptions}
119+
\begin{itemize}
120+
\item Catch exceptions by const reference
121+
\end{itemize}
122+
\end{goodpractice}
89123
\end{frame}
90124

91125
\begin{frame}[fragile]
92126
\frametitlecpp[98]{Exceptions}
93127
\begin{block}{Rethrowing exceptions}
94128
\begin{itemize}
95-
\item a caught exception can be rethrown
129+
\item a caught exception can be rethrown inside the catch handler
96130
\item useful when we want to act on an error, but cannot handle and want to propagate it
97131
\end{itemize}
98132
\end{block}
@@ -166,45 +200,17 @@
166200
\end{multicols}
167201
\end{frame}
168202

169-
\begin{frame}[fragile]
170-
\frametitlecpp[98]{Exceptions}
171-
\begin{block}{Standard exceptions}
172-
\begin{itemize}
173-
\item \cppinline{std::exception}, defined in header \cppinline{<exception>}
174-
\begin{itemize}
175-
\item Base class of all standard exceptions
176-
\item Get error message: \cppinline{virtual const char* what() const;}
177-
\item Please derive your own exception classes from this one
178-
\end{itemize}
179-
\item From \cppinline{<stdexcept>}:
180-
\begin{itemize}
181-
\item \cppinline{std::runtime_error}, \cppinline{std::logic_error}, \cppinline{std::out_of_range}, \cppinline{std::invalid_argument}, ...
182-
\item Store a string: \cppinline{throw std::runtime_error{"msg"}}
183-
\item You should use these the most
184-
\end{itemize}
185-
\item \cppinline{std::bad_alloc}, defined in header \cppinline{<new>}
186-
\begin{itemize}
187-
\item Thrown by standard allocation functions (e.g.\ \cppinline{new})
188-
\item Signals failure to allocate
189-
\item Carries no message
190-
\end{itemize}
191-
\item ...
192-
\end{itemize}
193-
\end{block}
194-
\end{frame}
195-
196203
\begin{frame}[fragile]
197204
\frametitlecpp[17]{Exceptions}
198205
\begin{goodpractice}{Exceptions}
199206
\begin{itemize}
200-
\item throw exceptions by value, catch them by (const) reference
201207
\item use exceptions for \textit{unlikely} runtime errors outside the program's control
202208
\begin{itemize}
203209
\item bad inputs, files unexpectedly not found, DB connection, \ldots
204210
\end{itemize}
205211
\item \textit{don't} use exceptions for logic errors in your code
206212
\begin{itemize}
207-
\item consider \cppinline{assert} and tests
213+
\item use \cppinline{assert} and tests
208214
\end{itemize}
209215
\item \textit{don't} use exceptions to provide alternative/skip return values
210216
\begin{itemize}
@@ -305,7 +311,7 @@
305311

306312
\begin{frame}[fragile]
307313
\frametitlecpp[11]{noexcept specifier}
308-
\begin{block}{}
314+
\begin{block}{\texttt{noexcept}}
309315
\begin{itemize}
310316
\item a function with the \cppinline{noexcept} specifier states that it guarantees to not throw an exception
311317
\begin{cppcode*}{gobble=2,linenos=false}
@@ -314,16 +320,20 @@
314320
\begin{itemize}
315321
\item either no exceptions is thrown or they are handled internally
316322
\item checked at compile time
317-
\item so allows the compiler to optimise around that knowledge
323+
\item allows the compiler to optimize around that knowledge
318324
\end{itemize}
319325
\item a function with \cppinline{noexcept(expression)} is only \cppinline{noexcept} when \cppinline{expression} evaluates to \cppinline{true} at compile-time
320326
\begin{cppcode*}{gobble=2,linenos=false}
321327
int safe_if_8B() noexcept(sizeof(long)==8);
322328
\end{cppcode*}
323-
\item Use \cppinline{noexcept} on leaf functions where you know the behaviour
324-
\item C++11 destructors are \cppinline{noexcept} - never throw from them
325329
\end{itemize}
326330
\end{block}
331+
\begin{goodpractice}{\texttt{noexcept}}
332+
\begin{itemize}
333+
\item Use \cppinline{noexcept} on leaf functions where you know the behavior
334+
\item C++11 destructors are \cppinline{noexcept} - never throw from them
335+
\end{itemize}
336+
\end{goodpractice}
327337
\end{frame}
328338

329339
\begin{advanced}

talk/morelanguage/lambda.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@
190190
\end{block}
191191
\end{frame}
192192

193+
\begin{advanced}
194+
193195
\begin{frame}[fragile]
194196
\frametitlecpp[11]{Capture list - this}
195197
\begin{block}{}
@@ -217,8 +219,6 @@
217219
Details in \href{https://www.nextptr.com/tutorial/ta1430524603/capture-this-in-lambda-expression-timeline-of-change}{this blog post}.
218220
\end{frame}
219221

220-
\begin{advanced}
221-
222222
\begin{frame}[fragile]
223223
\frametitlecpp[11]{Anatomy of a lambda}
224224
\begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}}

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