C++17 - The Complete Guide (C++17 - The Complete Guide)
This book is 88% complete
Last updated on 2019-02-16
About the Book
C++17 is the next evolution in modern C++ programming, which is already at least partially supported by the latest version of gcc, clang, and Visual C++. Although it is not as big a step as C++11, it contains a large number of small and valuable language and library features, which will change the way we program in C++. As usual, not everything is self-explanatory, combining new features gives even more power, and there are hidden traps.
This book presents all the new language and library features in C++17. It covers the motivation and context of each new feature with examples and background information. The focus is on how these features impact day-to-day programming, what it means to combine them, and how to benefit from this in practice.
Quotes:
"It's very clearly written and the examples are really helpful!"
Graham Markall
"I am thoroughly enjoying the book; I had skimmed the list of changes in C++17 several times before but until I started reading your book I had no idea how substantial the changes really were. It makes me wonder what I overlooked with previous updates!"
Jason Birch
"I thought I knew a bit about C++. I downloaded this book and learned something new in the *preface*. Highly recommended so far!"
Tristan Brindle
Buy early, pay less, free updates.
Note that this book will be published step-by-step.
It started with 270 pages first published in December 2018. Since then the contents grows with new chapters, examples, and caveats about the features of C++17 and I integrate all feedback I get for the pages already published.
The current version already covers the following topics, including how they play together in practice:
- Structured bindings, if/switch with initializers, inline variables, aggregate extensions, copy elision and materialization, lambda extensions,
- nested namespaces, defined expression evaluation order, relaxed enum initialization, relaxed static_assert, #__has_include
- class template argument deduction, compile-time if, fold expressions, strings and auto as template parameters, extended using, overloading lambdas
- optional<>, variant<>, any, byte, string_view
- the filesystem library
- some aspects of parallel algorithms
- type trait suffix _v, new type traits, and some library fixes
- new/delete for over-aligned data
- polymorphic memory resources (PMR)
See www.cppstd17.com for a detailed list of covered topics.
As written, once you bought it you will get all updates for free.
PDF versus Other Formats
I wrote the book in LaTeX and generated PDF myself directly (the way I did with all my other books). The other formats (epub, mobi, and online reading) come from the leanpub markdown interface, for which I generate the necessary input from LaTeX by script.
Thus, the PDF layout has a better quality than the other formats. For example, the syntax highlighting rules for the formats other than PDF have to get fixed as soon as possible and the index is missing yet. Leanpub and me are working on corresponding improvements.
I hope you anyway enjoy and benefit.
Nico
#cpp17tcg
Table of Contents
-
-
1. Preface
- 1.1 Versions of This Book
- 1.2 Acknowledgments
-
2. About This Book
- 2.1 What You Should Know Before Reading This Book
- 2.2 Overall Structure of the Book
- 2.3 How to Read This Book
- 2.4 The C++17 Standard
- 2.5 Example Code and Additional Information
- 2.6 Feedback
-
1. Preface
-
I Basic Language Features
-
3. Structured Bindings
- 3.1 Structured Bindings in Detail
-
3.2 Where Structured Bindings can be Used
- 3.2.1 Structures and Classes
- 3.2.2 Raw Arrays
-
3.2.3
std::pair
,std::tuple
, andstd::array
- 3.3 Providing a Tuple-Like API for Structured Bindings
- 3.4 Afternotes
-
4.
if
andswitch
with Initialization-
4.1
if
with Initialization -
4.2
switch
with Initialization - 4.3 Afternotes
-
4.1
-
5. Inline Variables
- 5.1 Motivation of Inline Variables
- 5.2 Using Inline Variables
-
5.3
constexpr
now impliesinline
-
5.4 Inline Variables and
thread_local
- 5.5 Afternotes
-
6. Aggregate Extensions
- 6.1 Motivation for Extended Aggregate Initialization
- 6.2 Using Extended Aggregate Initialization
- 6.3 Definition of Aggregates
- 6.4 Backward Incompatibilities
- 6.5 Afternotes
-
7. Mandatory Copy Elision or Passing Unmaterialized Objects
- 7.1 Motivation for Mandatory Copy Elision for Temporaries
- 7.2 Benefit of Mandatory Copy Elision for Temporaries
-
7.3 Clarified Value Categories
- 7.3.1 Value Categories
- 7.3.2 Value Categories Since C++17
- 7.4 Unmaterialized Return Value Passing
- 7.5 Afternotes
-
8. Lambda Extensions
-
8.1
constexpr
Lambdas -
8.2 Passing Copies of
this
to Lambdas - 8.3 Capturing by Reference
- 8.4 Afternotes
-
8.1
-
9. New Attributes and Attribute Features
-
9.1 Attribute
[[nodiscard]]
-
9.2 Attribute
[[maybe_unused]]
-
9.3 Attribute
[[fallthrough]]
- 9.4 General Attribute Extensions
- 9.5 Afternotes
-
9.1 Attribute
-
10. Other Language Features
- 10.1 Nested Namespaces
- 10.2 Defined Expression Evaluation Order
- 10.3 Relaxed Enum Initialization from Integral Values
-
10.4 Fixed Direct List Initialization with
auto
- 10.5 Hexadecimal Floating-Point Literals
- 10.6 UTF-8 Character Literals
- 10.7 Exception Specifications as Part of the Type
-
10.8 Single-Argument
static_assert
-
10.9 Preprocessor Condition
__has_include
- 10.10 Afternotes
-
3. Structured Bindings
-
II Template Features
-
11. Class Template Argument Deduction
-
11.1 Usage of Class Template Argument Deduction
- 11.1.1 Copying by Default
- 11.1.2 Deducing the Type of Lambdas
- 11.1.3 No Partial Class Template Argument Deduction
- 11.1.4 Class Template Argument Deduction Instead of Convenience Functions
-
11.2 Deduction Guides
- 11.2.1 Using Deduction Guides to Force Decay
- 11.2.2 Non-Template Deduction Guides
- 11.2.3 Deduction Guides versus Constructors
- 11.2.4 Explicit Deduction Guides
- 11.2.5 Deduction Guides for Aggregates
- 11.2.6 Standard Deduction Guides
- 11.3 Afternotes
-
11.1 Usage of Class Template Argument Deduction
-
12. Compile-Time
if
-
12.1 Motivation for Compile-Time
if
-
12.2 Using Compile-Time
if
-
12.2.1 Caveats for Compile-Time
if
-
12.2.2 Other Compile-Time
if
Examples
-
12.2.1 Caveats for Compile-Time
-
12.3 Compile-Time
if
with Initialization -
12.4 Using Compile-Time
if
Outside Templates - 12.5 Afternotes
-
12.1 Motivation for Compile-Time
-
13. Fold Expressions
- 13.1 Motivation for Fold Expressions
-
13.2 Using Fold Expressions
- 13.2.1 Dealing with Empty Parameter Packs
- 13.2.2 Supported Operators
- 13.2.3 Using Fold Expressions for Types
- 13.3 Afternotes
-
14. Dealing with Strings as Template Parameters
- 14.1 Using Strings in Templates
- 14.2 Afternotes
-
15. Placeholder Types like
auto
as Template Parameters-
15.1 Using
auto
as Template Parameter- 15.1.1 Parameterizing Templates for Characters and Strings
- 15.1.2 Defining Metaprogramming Constants
-
15.2 Using
auto
as Variable Template Parameter -
15.3 Using
decltype(auto)
as Template Parameter - 15.4 Afternotes
-
15.1 Using
-
16. Extended Using Declarations
- 16.1 Using Variadic Using Declarations
- 16.2 Variadic Using Declarations for Inheriting Constructors
- 16.3 Afternotes
-
11. Class Template Argument Deduction
-
III New Library Components
-
17.
std::optional<>
-
17.1 Using
std::optional<>
- 17.1.1 Optional Return Values
- 17.1.2 Optional Arguments and Data Members
-
17.2
std::optional<>
Types and Operations-
17.2.1
std::optional<>
Types -
17.2.2
std::optional<>
Operations
-
17.2.1
-
17.3 Special Cases
- 17.3.1 Optional of Boolean or Raw Pointer Values
- 17.3.2 Optional of Optional
- 17.4 Afternotes
-
17.1 Using
-
18.
std::variant<>
-
18.1 Motivation of
std::variant<>
-
18.2 Using
std::variant<>
-
18.3
std::variant<>
Types and Operations-
18.3.1
std::variant<>
Types -
18.3.2
std::variant<>
Operations - 18.3.3 Visitors
- 18.3.4 Valueless by Exception
-
18.3.1
-
18.4 Polymorphism and Inhomogeneous Collections with
std::variant
-
18.4.1 Geometric Objects with
std::variant
-
18.4.2 Other Inhomogeneous Collections with
std::variant
-
18.4.3 Comparing
variant
Polymorphism
-
18.4.1 Geometric Objects with
-
18.5 Special Cases with
std::variant<>
-
18.5.1 Having Both
bool
andstd::string
Alternatives
-
18.5.1 Having Both
- 18.6 Afternotes
-
18.1 Motivation of
-
19.
std::any
-
19.1 Using
std::any
-
19.2
std::any
Types and Operations- 19.2.1 Any Types
- 19.2.2 Any Operations
- 19.3 Afternotes
-
19.1 Using
-
20.
std::byte
-
20.1 Using
std::byte
-
20.2
std::byte
Types and Operations-
20.2.1
std::byte
Types -
20.2.2
std::byte
Operations
-
20.2.1
- 20.3 Afternotes
-
20.1 Using
-
21. String Views
-
21.1 Differences to
std::string
- 21.2 Using String Views
-
21.3 Using String Views Similar to Strings
- 21.3.1 String View Considered Harmful
-
21.4 String View Types and Operations
- 21.4.1 Concrete String View Types
- 21.4.2 String View Operations
- 21.4.3 String View Support by Other Types
-
21.5 Using String Views in API’s
- 21.5.1 Using String Views to Initialize Strings
- 21.5.2 Using String Views instead of Strings
- 21.6 Afternotes
-
21.1 Differences to
-
22. The Filesystem Library
-
22.1 Basic Examples
- 22.1.1 Print Attributes of a Passed Filesystem Path
- 22.1.2 Switch Over Filesystem Types
- 22.1.3 Create Different Types of Files {#createfiles.cpp}
- 22.1.4 Dealing with Filesystems Using Parallel Algorithms
-
22.2 Principles and Terminology
- 22.2.1 General Portability Disclaimer
- 22.2.2 Namespace
- 22.2.3 Paths
- 22.2.4 Normalization
- 22.2.5 Member versus Free-Standing Functions
- 22.2.6 Error Handling
- 22.2.7 File Types
-
22.3 Path Operations
- 22.3.1 Path Creation
- 22.3.2 Path Inspection
- 22.3.3 Path I/O and Conversions
- 22.3.4 Conversions Between Native and Generic Format
- 22.3.5 Path Modifications
- 22.3.6 Path Comparisons
- 22.3.7 Other Path Operations
-
22.4 Filesystem Operations
- 22.4.1 File Attributes
- 22.4.2 File Status
- 22.4.3 Permissions
- 22.4.4 Filesystem Modifications
- 22.4.5 Symbolic Links and Filesystem-Dependent Path Conversions
- 22.4.6 Other Filesystem Operations
-
22.5 Iterating Over Directories
- 22.5.1 Directory Entries
- 22.6 Afternotes
-
22.1 Basic Examples
-
17.
-
IV Library Extensions and Modifications
-
23. Type Traits Extensions
-
23.1 Type Traits Suffix
_v
- 23.2 New Type Traits
-
23.3
std::bool_constant<>
-
23.4
std::void_t<>
- 23.5 Afternotes
-
23.1 Type Traits Suffix
-
24. Parallel STL Algorithms
-
24.1 Using Parallel Algorithms
-
24.1.1 Using a Parallel
for_each()
-
24.1.2 Using a Parallel
sort()
-
24.1.1 Using a Parallel
- 24.2 Execution Policies
- 24.3 Exception Handling
- 24.4 Benefit of not using Parallel Algorithms
- 24.5 Overview of Parallel Algorithms
-
24.6 New Algorithms for Parallel Processing
-
24.6.1
reduce()
-
24.6.1
- 24.7 Parallel Algorithms in Detail
- 24.8 Afternotes
-
24.1 Using Parallel Algorithms
-
25. Substring and Subsequence Searchers
-
25.1 Using Substring Searchers
-
25.1.1 Using Searchers with
search()
- 25.1.2 Using Searchers Directly
-
25.1.1 Using Searchers with
- 25.2 Using General Subsequence Searchers
- 25.3 Using Searcher Predicates
- 25.4 Afternotes
-
25.1 Using Substring Searchers
-
26. Other Utility Functions and Algorithms
-
26.1
size()
,empty()
, anddata()
-
26.1.1 Generic
size()
Function -
26.1.2 Generic
empty()
Function -
26.1.3 Generic
data()
Function
-
26.1.1 Generic
-
26.2
as_const()
- 26.2.1 Capturing by Const Reference
-
26.3
clamp()
-
26.4
sample()
-
26.5
for_each_n()
- 26.6 Afternotes
-
26.1
-
27. Container Extensions
- 27.1 Container-Support of Incomplete Types
- 27.2 Node Handles
- 27.3 Afternotes
-
28. Multi-Threading and Concurrency
-
28.1 Supplementary Mutexes and Locks
-
28.1.1
std::scoped_lock
-
28.1.2
std::shared_mutex
-
28.1.1
-
28.2
is_always_lock_free()
for Atomics - 28.3 Cache-Line Sizes
- 28.4 Afternotes
-
28.1 Supplementary Mutexes and Locks
-
23. Type Traits Extensions
-
V Expert Utilities
-
29. Polymorphic Memory Resources (PMR)
-
29.1 Using Standard Memory Resources
- 29.1.1 Motivating Example
- 29.1.2 Standard Memory Resources
- 29.1.3 Standard Memory Resources in Detail
-
29.2 Defining Custom Memory Resources
- 29.2.1 Equality of Memory Resources
-
29.3 Providing Memory Resources Support for Custom Types
- 29.3.1 Definition of a PMR Type
- 29.3.2 Usage of a PMR Type
- 29.3.3 Dealing with the Different Types
- 29.4 Afternotes
-
29.1 Using Standard Memory Resources
-
30.
new
anddelete
with Over-Aligned Data-
30.1 Using
new
with Alignments- 30.1.1 Distinct Dynamic/Heap Memory Arenas
-
30.1.2 Passing the Alignment with the
new
Expression
-
30.2 Implementing
operator new()
for Aligned Memory- 30.2.1 Implementing Aligned Allocation Before C++17
-
30.2.2 Implementing Type-Specific
operator new()
-
30.3 Implementing Global
operator new()
- 30.3.1 Backward Incompatibilities
-
30.4 Tracking all
::new
Calls - 30.5 Afternotes
-
30.1 Using
-
31. Other Library Improvements for Experts
-
31.1 Low-Level Conversions between Character Sequences and Numeric Values
- 31.1.1 Example Usage
- 31.1.2 Floating-Point Round-Trip Support
- 31.2 Afternotes
-
31.1 Low-Level Conversions between Character Sequences and Numeric Values
-
Glossary
-
B
- bitmask type
-
F
- full specialization
-
I
- incomplete type
-
P
- partial specialization
-
S
- small/short string optimization (SSO)
-
V
- variable template
- variadic template
-
B
- Index
-
29. Polymorphic Memory Resources (PMR)
- Notes
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...