C++17 The Complete Guide

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

All the new language and library features of C++17. C++17 is the next evolution in modern C++ programming, now supported by major c++ compilers g++, clang, and Visual C++. Although adopting C++17 it is not as big a step as adopting C++11, C++17 does contain a large number of valuable language and library features, which support and change the way we program in C++. As usual, not everything is self-explanatory, combining new features gives you even more power, and there are hidden traps.The book covers the motivation for and context of all new C++17 features (covering both language and library) with many examples and unique background information for application programmers as well as library developers. The focus is on how these features impact day-to-day programming, what effect combining features can have, and how you can benefit from them in practice.

Author(s): Nicolai M. Josuttis
Publisher: Lean
Year: 2019

Language: English
Pages: 390
Tags: C++

C++17 - The Complete Guide......Page 1
Contents......Page 3
Preface......Page 13
What You Should Know Before Reading This Book......Page 17
How to Read This Book......Page 18
Feedback......Page 19
Part I: Basic Language Features......Page 21
1 Structured Bindings......Page 23
1.1 Structured Bindings in Detail......Page 24
1.2.1 Structures and Classes......Page 28
1.2.3 std::pair, std::tuple, and std::array......Page 29
1.3 Providing a Tuple-Like API for Structured Bindings......Page 31
1.4 Afternotes......Page 38
2.1 if with Initialization......Page 39
2.3 Afternotes......Page 41
3.1 Motivation of Inline Variables......Page 43
3.2 Using Inline Variables......Page 45
3.3 constexpr now implies inline......Page 46
3.4 Inline Variables and thread_local......Page 47
3.5 Afternotes......Page 49
4 Aggregate Extensions......Page 51
4.2 Using Extended Aggregate Initialization......Page 52
4.4 Backward Incompatibilities......Page 54
4.5 Afternotes......Page 55
5.1 Motivation for Mandatory Copy Elision for Temporaries......Page 57
5.2 Benefit of Mandatory Copy Elision for Temporaries......Page 59
5.3.1 Value Categories......Page 60
5.3.2 Value Categories Since C++17......Page 62
5.4 Unmaterialized Return Value Passing......Page 63
5.5 Afternotes......Page 64
6.1 constexpr Lambdas......Page 65
6.2 Passing Copies of this to Lambdas......Page 67
6.4 Afternotes......Page 69
7.1 Attribute [[nodiscard]]......Page 71
7.2 Attribute [[maybe_unused]]......Page 72
7.3 Attribute [[fallthrough]]......Page 73
7.5 Afternotes......Page 74
8.1 Nested Namespaces......Page 77
8.2 Defined Expression Evaluation Order......Page 78
8.3 Relaxed Enum Initialization from Integral Values......Page 81
8.4 Fixed Direct List Initialization with auto......Page 82
8.5 Hexadecimal Floating-Point Literals......Page 83
8.6 UTF-8 Character Literals......Page 84
8.7 Exception Specifications as Part of the Type......Page 85
8.8 Single-Argument static_assert......Page 88
8.10 Afternotes......Page 89
Part II: Template Features......Page 91
9 Class Template Argument Deduction......Page 93
9.1 Usage of Class Template Argument Deduction......Page 94
9.1.1 Copying by Default......Page 95
9.1.2 Deducing the Type of Lambdas......Page 96
9.1.3 No Partial Class Template Argument Deduction......Page 97
9.1.4 Class Template Argument Deduction Instead of Convenience Functions......Page 98
9.2 Deduction Guides......Page 100
9.2.2 Non-Template Deduction Guides......Page 101
9.2.3 Deduction Guides versus Constructors......Page 102
9.2.5 Deduction Guides for Aggregates......Page 103
9.2.6 Standard Deduction Guides......Page 104
9.3 Afternotes......Page 109
10 Compile-Time if......Page 111
10.1 Motivation for Compile-Time if......Page 112
10.2.1 Caveats for Compile-Time if......Page 114
10.2.2 Other Compile-Time if Examples......Page 117
10.4 Using Compile-Time if Outside Templates......Page 120
10.5 Afternotes......Page 122
11 Fold Expressions......Page 123
11.2 Using Fold Expressions......Page 124
11.2.1 Dealing with Empty Parameter Packs......Page 126
11.2.2 Supported Operators......Page 129
11.2.3 Using Fold Expressions for Types......Page 132
11.3 Afternotes......Page 133
12.1 Using Strings in Templates......Page 135
12.2 Afternotes......Page 136
13.1 Using auto as Template Parameter......Page 137
13.1.1 Parameterizing Templates for Characters and Strings......Page 138
13.1.2 Defining Metaprogramming Constants......Page 139
13.2 Using auto as Variable Template Parameter......Page 140
13.3 Using decltype(auto) as Template Parameter......Page 142
13.4 Afternotes......Page 143
14.1 Using Variadic Using Declarations......Page 145
14.2 Variadic Using Declarations for Inheriting Constructors......Page 146
14.3 Afternotes......Page 148
Part III: New Library Components......Page 149
15.1 Using std::optional<>......Page 151
15.1.1 Optional Return Values......Page 152
15.1.2 Optional Arguments and Data Members......Page 153
15.2.2 std::optional<> Operations......Page 155
15.3.1 Optional of Boolean or Raw Pointer Values......Page 160
15.4 Afternotes......Page 161
16.1 Motivation of std::variant<>......Page 163
16.2 Using std::variant<>......Page 164
16.3 std::variant<> Types and Operations......Page 166
16.3.2 std::variant<> Operations......Page 167
16.3.3 Visitors......Page 171
16.3.4 Valueless by Exception......Page 175
16.4.1 Geometric Objects with std::variant......Page 176
16.4.2 Other Inhomogeneous Collections with std::variant......Page 179
16.4.3 Comparing variant Polymorphism......Page 180
16.5.1 Having Both bool and std::string Alternatives......Page 181
16.6 Afternotes......Page 182
17.1 Using std::any......Page 183
17.2.2 Any Operations......Page 186
17.3 Afternotes......Page 189
18.1 Using std::byte......Page 191
18.2.2 std::byte Operations......Page 193
18.3 Afternotes......Page 195
19.1 Differences to std::string......Page 197
19.3 Using String Views Similar to Strings......Page 198
19.3.1 String View Considered Harmful......Page 200
19.4.2 String View Operations......Page 204
19.4.3 String View Support by Other Types......Page 207
19.5.1 Using String Views to Initialize Strings......Page 208
19.5.2 Using String Views instead of Strings......Page 210
19.6 Afternotes......Page 211
20.1.1 Print Attributes of a Passed Filesystem Path......Page 213
20.1.2 Switch Over Filesystem Types......Page 216
20.1.3 Create Different Types of Files......Page 218
20.1.4 Dealing with Filesystems Using Parallel Algorithms......Page 222
20.2.3 Paths......Page 223
20.2.5 Member versus Free-Standing Functions......Page 225
20.2.6 Error Handling......Page 226
20.2.7 File Types......Page 228
20.3 Path Operations......Page 229
20.3.2 Path Inspection......Page 230
20.3.3 Path I/O and Conversions......Page 233
20.3.4 Conversions Between Native and Generic Format......Page 236
20.3.5 Path Modifications......Page 238
20.3.6 Path Comparisons......Page 240
20.4.1 File Attributes......Page 242
20.4.2 File Status......Page 246
20.4.3 Permissions......Page 248
20.4.4 Filesystem Modifications......Page 250
20.4.5 Symbolic Links and Filesystem-Dependent Path Conversions......Page 253
20.4.6 Other Filesystem Operations......Page 256
20.5 Iterating Over Directories......Page 257
20.5.1 Directory Entries......Page 258
20.6 Afternotes......Page 261
Part IV: Library Extensions and Modifications......Page 263
21.1 Type Traits Suffix _v......Page 265
21.3 std::bool_constant<>......Page 266
21.4 std::void_t<>......Page 268
21.5 Afternotes......Page 270
22 Parallel STL Algorithms......Page 271
22.1.1 Using a Parallel for_each()......Page 272
22.1.2 Using a Parallel sort()......Page 275
22.2 Execution Policies......Page 277
22.5 Overview of Parallel Algorithms......Page 278
22.6.1 reduce()......Page 282
22.8 Afternotes......Page 291
23.1.1 Using Searchers with search()......Page 293
23.1.2 Using Searchers Directly......Page 295
23.2 Using General Subsequence Searchers......Page 296
23.3 Using Searcher Predicates......Page 297
23.4 Afternotes......Page 298
24.1.1 Generic size() Function......Page 299
24.1.3 Generic data() Function......Page 301
24.2.1 Capturing by Const Reference......Page 302
24.3 clamp()......Page 303
24.4 sample()......Page 304
24.5 for_each_n()......Page 308
24.6 Afternotes......Page 309
25.1 Container-Support of Incomplete Types......Page 311
25.2 Node Handles......Page 313
25.3 Afternotes......Page 314
26.1.1 std::scoped_lock......Page 317
26.2 is_always_lock_free() for Atomics......Page 319
26.3 Cache-Line Sizes......Page 321
26.4 Afternotes......Page 322
Part V: Expert Utilities......Page 323
27 Polymorphic Memory Resources (PMR)......Page 325
27.1.1 Motivating Example......Page 326
27.1.2 Standard Memory Resources......Page 331
27.1.3 Standard Memory Resources in Detail......Page 333
27.2 Defining Custom Memory Resources......Page 339
27.2.1 Equality of Memory Resources......Page 343
27.3.1 Definition of a PMR Type......Page 344
27.3.2 Usage of a PMR Type......Page 347
27.3.3 Dealing with the Different Types......Page 348
27.4 Afternotes......Page 349
28 new and delete with Over-Aligned Data......Page 351
28.1.1 Distinct Dynamic/Heap Memory Arenas......Page 352
28.1.2 Passing the Alignment with the new Expression......Page 353
28.2.1 Implementing Aligned Allocation Before C++17......Page 354
28.2.2 Implementing Type-Specific operator new()......Page 357
28.3 Implementing Global operator new()......Page 363
28.3.1 Backward Incompatibilities......Page 364
28.4 Tracking all ::new Calls......Page 365
28.5 Afternotes......Page 368
29.1 Low-Level Conversions between Character Sequences and Numeric Values......Page 369
29.1.1 Example Usage......Page 370
29.1.2 Floating-Point Round-Trip Support......Page 372
29.2 Afternotes......Page 374
P......Page 377
V......Page 378
A......Page 379
C......Page 380
E......Page 381
G......Page 382
I......Page 383
M......Page 384
P......Page 385
R......Page 386
T......Page 387
V......Page 388
X......Page 389