-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew_library_features.tex
418 lines (386 loc) · 16.8 KB
/
New_library_features.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
%-------------------------------%
% Author: Alessandro Sciarra %
% Date: 15 Jun 2022 %
%-------------------------------%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{C++17 new library features I will \textbf{NOT} cover}
\hspace*{5mm}
\begin{tabular}{l}
\URL[PB]{https://en.cppreference.com/w/cpp/numeric/special_functions}{~Mathematical special functions} (still \URL[PT]{https://en.cppreference.com/w/cpp/compiler_support/17\#C.2B.2B17_library_features}{GNU compiler only})\\[1mm]
\URL[PB]{https://en.cppreference.com/w/cpp/types/byte}{~\CPP|std::byte|}\\[1mm]
\URL[PB]{https://en.cppreference.com/w/cpp/utility/functional/invoke}{~\CPP|std::invoke|}\Remark{More understandable \URL[PB]{https://stackoverflow.com/q/43680182/14967071}{SO question}}\\[1mm]
\URL[PB]{https://en.cppreference.com/w/cpp/utility/apply}{~\CPP|std::apply|} $\;\to\;$ {\footnotesize\URL[PB]{https://stackoverflow.com/q/52449163/14967071}{What's the difference with \CPP|std::invoke|\,?}}\\[1mm]
\URL[PB]{https://wg21.link/P0083R3}{~Splicing maps and sets} \Remark{i.e. effectively transfer elements \`a la \CPP|std::list::splice|}
\end{tabular}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}{C++17 new library features I will cover}
\vspace{-3mm}
\hspace*{1cm}
\begin{minipage}[t][0.65\textheight]{\textwidth}
\tableofcontents
\end{minipage}
\end{frame}
%============================================%
%============================================%
\section{Throwing in a destructor}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead: Is it still bad practice?}
\begin{itemize}
\item It has been considered bad practice for years\\
\then \URL[ALERT]{http://www.gotw.ca/gotw/047.htm}{Blog from Herb Sutter (2009)}
\item \CPP|bool std::uncaught_exception()|\\
detects only if the current thread has a live exception object\\
\then it fails to cover all use cases! \Remark{Deprecated in C++17, removed in C++20}
\item C++17 introduces \CPP|int std::uncaught_exceptions()|\\
that says how many uncaught exceptions exist\\
\then see \URL[PB]{https://stackoverflow.com/a/49503194/14967071}{this SO answer} for a nice example
\end{itemize}
\begin{varblock}{}[0.68\textwidth]{If you need it}
You can now safely throw from a destructor!
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{\CPP|std::string\_view|}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item A \alert{non-owning} reference to a string, it is just a view!
\item Useful for providing an abstraction on top of strings \Remark{e.g.\ for parsing}
\item There are view modification functions which do not affect the viewed string
\item \CPP|std::string_view| \alert{doesn't use null terminators} to mark the end of the string
\item \PP{Prefer passing string views by value to constant refs to strings!}\\
\then \URL[PB]{https://stackoverflow.com/a/40129198/14967071}{How is it faster?}
\item More information: \URL[PB]{https://en.cppreference.com/w/cpp/string/basic_string_view}{C++ reference}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Basic usage}<only@1>
\begin{Cpp}
#include |+<iostream>+|
#include |+<string>+|
#include |+<string_view>+|
int main()
{
std::string str {" trim me"};
std::string_view v {str};
v.remove_prefix(std::min(
v.find_first_not_of(" "), v.size()));
std::cout << '_' << str << "_\t_" << v << "_\n";
using namespace std::literals;
auto getSV = [](){ // Noooooo! -> view is NOT OWNING!
std::string x = "local";
return std::string_view{x};
};
std::string_view bad = getSV();
std::cout << bad << '\n'; // UNDEFINED BEHAVIOUR
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+_ trim me_ _trim me_
p4Ek+|
\end{Bash}
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{\CPP|std::variant|}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item A class template that represents a type-safe union
\item An instance of \,\CPP|std::variant|\, at any given time either holds a value of one of its alternative types, or in the case of error, no value \Remark{generally hard to achieve}
\item Assign values at construction or using the \CPP|operator=|
\item Retrieve values using the \,\CPP|std::get<T>()|\, function
\item \alert{Revolution in inheritance patterns (e.g. visitor)}
\item Use the \URL[PB]{https://en.cppreference.com/w/cpp/utility/variant/visit}{non-member function} \,\CPP|std::visit|\, to apply a callable to the value hold by variant(s)
\item More information: \URL[PB]{https://en.cppreference.com/w/cpp/utility/variant}{C++ reference}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Basic usage (\overlaynumber)}<only@1>
\begin{Cpp}
#include |+<variant>+|
#include |+<string>+|
#include |+<iostream>+|
int main()
{
std::variant<int, float> v, w;
v = 42; // v contains int
int i = std::get<int>(v);
assert(42 == i); // succeeds
w = std::get<int>(v);
w = std::get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line
std::variant<std::string, float> v{
std::in_place_type<std::string>, 4, 'A'};
// initializes 1st alternative with std::string{4, 'A'};
assert(v.index() == 0);
std::cout << "v=" << std::get<std::string>(v) << '\n';
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+v=AAAA+|
\end{Bash}
\end{varblock}
\begin{varblock}{example}[\textwidth]{Basic usage (\overlaynumber)}<only@2>
\begin{Cpp}
#include |+<variant>+|
#include |+<iostream>+|
int main()
{
std::variant<int, float> v, w;
v = 42;
w = v;
// Errors:
//std::get<double>(v); // no double in [int, float]
//std::get<3>(v); // valid index values are 0 and 1
try {
// w contains int, not float: will throw
std::get<float>(w);
}
catch (const std::bad_variant_access& e) {
std::cout << e.what() << '\n';
}
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+bad_variant_access+|
\end{Bash}
\end{varblock}
\begin{varblock}{example}[\textwidth]{Basic usage (\overlaynumber)}<only@3>
\begin{Cpp}
#include |+<variant>+|
#include |+<iostream>+|
#include |+<vector>+|
using var_t = std::variant<int, long, double, std::string>;
int main()
{
std::vector<var_t> vec = {10, 15l, 1.6, "hello"};
for (auto& v: vec) {
// value-returning visitor, returning another variant
var_t w = std::visit([](auto&& arg) -> var_t {
return arg + arg;
}, v);
// void visitor, only called for side-effects
std::visit([](auto&& arg){
std::cout << arg << " ";
}, w);
}
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+20 30 3.2 hellohello+|
\end{Bash}
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{\CPP|std::optional|}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item A class template that manages an optional contained value, i.e.\ a value that may or may not be present
\item A common use case for optional is the return value of a function that may fail
\item No dynamic memory allocation ever takes place\\
\then \alert{an optional object models an object, not a pointer}
\item Possible to contextually convert to \CPP|bool|
\item \CPP|std::nullopt| is used to indicate optional type with uninitialized state
\item Use \CPP|std::make_optional<T>| if needed
\item More information: \URL[PB]{https://en.cppreference.com/w/cpp/utility/optional}{C++ reference}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Optional returning value}<only@1>
\begin{Cpp}
#include |+<iostream>+|
#include |+<optional>+|
#include |+<sstream>+|
std::optional<int> to_int(const std::string& s) {
std::optional<int> oi{};
int i;
if(std::stringstream stm(s); stm >> i)
if(stm.get() == std::char_traits<char>::eof())
oi = i;
return oi;
}
int main()
{
auto number = to_int("-42");
std::cout << *number << " == " << number.value() << '\n';
// *opt is UB if opt does not contain a value!!
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+-42 == -42 +|
\end{Bash}
\end{varblock}
\begin{varblock}{example}[\textwidth]{Optional arguments}<only@2>
\begin{Cpp}
#include |+<iostream>+|
#include |+<optional>+|
#include |+<string_view>+|
auto slice(std::string_view str,
std::optional<int> start,
std::optional<int> finish)
{
auto a = start.value_or(0);
auto b = end.value_or(str.size());
return str.substr(a, b-a);
}
int main()
{
std::cout
<< slice("Hello world!", 6, std::nullopt) << '\n'
<< slice("Hello world!", std::nullopt, 5) << '\n';
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+world!
Hello+|
\end{Bash}
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{\CPP|std::any|}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item A type-safe, single-value container\\ for any \textbf{copy-constructible type}.
\item Use \CPP|operator=| to assign value
\item Use \CPP|std::any_cast<T>| to retrieve value
\item Use \CPP|std::make_any<T>| in generic programming
\item More information: \URL[PB]{}{C++ reference}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Basic usage}<only@1>
\begin{Cpp}
#include |+<any>+|
#include |+<iostream>+|
int main()
{
std::any a = 1;
std::cout << '[' << std::any_cast<int>(a) << ", ";
a = 3.14;
std::cout << std::any_cast<double>(a) << ", ";
a = true;
std::cout << std::boolalpha
<< std::any_cast<bool>(a) << "]\n";
try {
std::cout << std::any_cast<float>(a) << '\n';
}
catch (const std::bad_any_cast& e) {
std::cout << e.what() << '\n';
}
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+[1, 3.14, true]
bad any cast+|
\end{Bash}
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{\CPP|std::filesystem|}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item This library was originally developed as \URL[PB]{http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm}{boost.filesystem}
\item It provides a standard way to manipulate \textbf{file objects}
\begin{itemize}
\item directory
\item regular file
\item symbolic link
\item other special file types (block, character, fifo, socket)
\end{itemize}
\textbf{file names} and \textbf{paths} in a filesystem.
\item \PP{Many additional features are available!}
\item More information: \URL[PB]{https://en.cppreference.com/w/cpp/filesystem}{C++ reference}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Copy big file if existing and enough memory is available}<only@1>
\begin{Cpp}
#include |+<iostream>+|
#include |+<filesystem>+|
namespace fs = std::filesystem;
int main()
{
const auto filePath {"bigFileToCopy"};
if (fs::exists(filePath)) {
const auto fileSize {fs::file_size(filePath)};
fs::path tmpPath {"/tmp"};
if (fs::space(tmpPath).available > fileSize)
{
fs::create_directory(tmpPath.append("example"));
fs::copy_file(filePath, tmpPath.append("new"));
}
} else
std::cout
<< "File \"" << filePath << "\" not found.\n";
}
\end{Cpp}
\begin{Bash}[numbers=none]
|+File "bigFileToCopy" not found.+|
\end{Bash}
\end{varblock}
\end{frame}
%============================================%
%============================================%
\section{Parallel algorithms}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{\insertsectionhead}
\vspace{-3mm}
\begin{itemize}
\item Many of the STL algorithms started to support the parallel execution policies of the following possible types:
\begin{itemize}
\item \CPP|sequenced_policy|
\item \CPP|parallel_policy|
\item \CPP|parallel_unsequenced_policy|
\end{itemize}
\item \alert{When using parallel execution policy, it is the programmer's responsibility to avoid data races and deadlocks}
\item More information: \URL[PB]{https://en.cppreference.com/w/cpp/algorithm\#Execution_policies}{C++ reference}
\item Unfortunately \URL[PQ]{https://en.cppreference.com/w/cpp/compiler_support/17\#C.2B.2B17_library_features}{(Apple) Clang does not support them yet}
\end{itemize}
\end{frame}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
\begin{frame}[fragile]{}
\begin{varblock}{example}[\textwidth]{Untested code}<only@1>
\begin{Cpp}
#include |+<algorithm>+|
#include |+<vector>+|
#include |+<execution>+|
int main()
{
int a[] = {0,1};
std::vector<int> v;
std::for_each(std::execution::par,
std::begin(a), std::end(a),
[&](int i){
v.push_back(i*2+1); // Error: data race
});
std::vector<int> longVector /* Initialization */;
// Find element using parallel execution policy
auto result = std::find(std::execution::par,
std::begin(longVector),
std::end(longVector),
2);
}
\end{Cpp}
\end{varblock}
\end{frame}
%============================================%