C++17 - The Complete Guide (C++17 - The Complete Guide)
C++17 - The Complete Guide
About the Book
C++17 is the next evolution in modern C++ programming, which is now 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 of 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.
Testimonials:
"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 was 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 now covers all new features of C++17, both core language and library features.
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 write the book in LaTeX and generate PDF from it (the way I wrote 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 enjoy and benefit.
Nico
#cpp17tcg
Bundles that include this book
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 Error Terminology
- 2.5 The C++17 Standard
- 2.6 Example Code and Additional Information
- 2.7 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 for Inline Variables
- 5.2 Using Inline Variables
-
5.3
constexpr
Now Impliesinline
For Static Members -
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.1.1 Using
constexpr
Lambdas
-
8.1.1 Using
-
8.2 Passing Copies of
this
to Lambdas -
8.3 Capturing by
const
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 Use 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 Use 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 String Literals as Template Parameters
- 14.1 Using Strings in Templates
- 14.2 Afternotes
-
15. Placeholder Types like
auto
as Template Parameters-
15.1 Using
auto
for Template Parameters- 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 for
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 Heterogeneous Collections with
std::variant
-
18.4.1 Geometric Objects with
std::variant
-
18.4.2 Other Heterogeneous 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 for
-
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 Compared to
std::string
- 21.2 Using String Views
-
21.3 Using String Views as Parameters
- 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 APIs
- 21.5.1 Using String Views instead of Strings
- 21.6 Afternotes
-
21.1 Differences Compared 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 Function 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. Extensions of Type Traits
-
23.1 Type Traits Suffix
_v
- 23.2 New Type Traits
- 23.3 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 Motivation for New Algorithms for Parallel Processing
-
24.6.1
reduce()
-
24.6.1
- 24.7 Afternotes
-
24.1 Using Parallel Algorithms
-
25. New STL Algorithms in Detail
-
25.1
std::for_each_n()
-
25.2 New Numeric STL Algorithms
-
25.2.1
std::reduce()
-
25.2.2
std::transform_reduce()
-
25.2.3
std::inclusive_scan()
andstd::exclusive_scan()
-
25.2.4
std::transform_inclusive_scan()
andstd::transform_exclusive_scan()
-
25.2.1
- 25.3 Afternotes
-
25.1
-
26. Substring and Subsequence Searchers
-
26.1 Using Substring Searchers
-
26.1.1 Using Searchers with
search()
- 26.1.2 Using Searchers Directly
-
26.1.1 Using Searchers with
- 26.2 Using General Subsequence Searchers
- 26.3 Using Searcher Predicates
- 26.4 Afternotes
-
26.1 Using Substring Searchers
-
27. Other Utility Functions and Algorithms
-
27.1
size()
,empty()
, anddata()
-
27.1.1 Generic
size()
Function -
27.1.2 Generic
empty()
Function -
27.1.3 Generic
data()
Function
-
27.1.1 Generic
-
27.2
as_const()
- 27.2.1 Capturing by Const Reference
-
27.3
clamp()
-
27.4
sample()
- 27.5 Afternotes
-
27.1
-
28. Container and String Extensions
-
28.1 Node Handles
- 28.1.1 Modifying a Key
- 28.1.2 Moving Nodes Between Containers
- 28.1.3 Merging Containers
-
28.2 Emplace Improvements
- 28.2.1 Return Type of Emplace Functions
-
28.2.2
try_emplace()
andinsert_or_assign()
for Maps
- 28.3 Container Support for Incomplete Types
- 28.4 String Improvements
- 28.5 Afternotes
-
28.1 Node Handles
-
29. Multi-Threading and Concurrency
-
29.1 Supplementary Mutexes and Locks
-
29.1.1
std::scoped_lock
-
29.1.2
std::shared_mutex
-
29.1.1
-
29.2
is_always_lock_free
for Atomics - 29.3 Cache Line Sizes
- 29.4 Afternotes
-
29.1 Supplementary Mutexes and Locks
-
30. Other Small Library Features and Modifications
-
30.1
std::uncaught_exceptions()
-
30.2 Shared Pointer Improvements
- 30.2.1 Special handling for Shared Pointers to Raw C Arrays
-
30.2.2
reinterpret_pointer_cast
for Shared Pointers -
30.2.3
weak_type
for Shared Pointers -
30.2.4
weak_from_this
for Shared Pointers
-
30.3 Numeric Extensions
- 30.3.1 Greatest Common Divisor and Least Common Multiple
-
30.3.2 Three-Argument Overloads of
std::hypot()
- 30.3.3 Mathematical Special Functions
-
30.4
chrono
Extensions -
30.5
constexpr
Extensions and Fixes -
30.6
noexcept
Extensions and Fixes - 30.7 Afternotes
-
30.1
-
23. Extensions of Type Traits
-
V Expert Utilities
-
31. Polymorphic Memory Resources (PMR)
-
31.1 Using Standard Memory Resources
- 31.1.1 Motivating Example
- 31.1.2 Standard Memory Resources
- 31.1.3 Standard Memory Resources in Detail
-
31.2 Defining Custom Memory Resources
- 31.2.1 Equality of Memory Resources
-
31.3 Providing Memory Resource Support for Custom Types
- 31.3.1 Definition of a PMR Type
- 31.3.2 Using a PMR Type
- 31.3.3 Dealing with the Different Types
- 31.4 Afternotes
-
31.1 Using Standard Memory Resources
-
32.
new
anddelete
with Over-Aligned Data-
32.1 Using
new
with Alignments- 32.1.1 Distinct Dynamic/Heap Memory Arenas
-
32.1.2 Passing the Alignment with the
new
Expression
-
32.2 Implementing
operator new()
for Aligned Memory- 32.2.1 Implementing Aligned Allocation Before C++17
-
32.2.2 Implementing Type-Specific
operator new()
-
32.3 Implementing Global
operator new()
- 32.3.1 Backward Incompatibilities
-
32.4 Tracking All
::new
Calls - 32.5 Afternotes
-
32.1 Using
-
33.
std::to_chars()
andstd::from_chars()
- 33.1 Motivation for Low-Level Conversions between Character Sequences and Numeric Values
-
33.2 Example Usage
-
33.2.1
from_chars()
-
33.2.2
to_chars()
-
33.2.1
- 33.3 Floating-Point Round-Trip Support
- 33.4 Afternotes
-
34.
std::launder()
-
34.1 Motivation for
std::launder()
-
34.2 How
launder()
Solves the Problem -
34.3 Why/When
launder()
Does Not Work - 34.4 Afternotes
-
34.1 Motivation for
-
35. Improvements for Implementing Generic Code
-
35.1
std::invoke<>()
-
35.2
std::bool_constant<>
-
35.3
std::void_t<>
- 35.4 Afternotes
-
35.1
-
31. Polymorphic Memory Resources (PMR)
-
VI Final General Hints
-
36. Common C++17 Settings
-
36.1 Value of
__cplusplus
- 36.2 Compatibility to C11
- 36.3 Dealing with Signal Handlers
- 36.4 Forward Progress Guarantees
- 36.5 Afternotes
-
36.1 Value of
-
37. Deprecated and Removed Features
-
37.1 Deprecated and Removed Core Language Features
- 37.1.1 Throw Specifications
-
37.1.2 Keyword
register
-
37.1.3 Disable
++
forbool
- 37.1.4 Trigraphs
-
37.1.5 Definition/Redeclaration of
static
constexpr
Members
-
37.2 Deprecated and Removed Library Features
-
37.2.1
auto_ptr
-
37.2.2 Algorithm
random_shuffle()
-
37.2.3
unary_function
andbinary_function
-
37.2.4
ptr_fun()
,mem_fun()
, and Binders -
37.2.5 Allocator Support for
std::function<>
- 37.2.6 Deprecated IOStream Aliases
- 37.2.7 Deprecated Library Features
-
37.2.1
- 37.3 Afternotes
-
37.1 Deprecated and Removed Core Language Features
-
38. Glossary
-
38.1 B
- 38.1.1 bitmask type
-
38.2 F
- 38.2.1 full specialization
-
38.3 I
- 38.3.1 incomplete type
-
38.4 P
- 38.4.1 partial specialization
-
38.5 S
- 38.5.1 small/short string optimization (SSO)
-
38.6 V
- 38.6.1 variable template
- 38.6.2 variadic template
-
38.1 B
- 39. Index
-
36. Common C++17 Settings
- Notes
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,595,069writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++20 - The Complete Guide
Nicolai M. JosuttisAll the new language and library features of C++20 (for those who know previous versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
SignalR on .NET 6 - the Complete Guide
Fiodar SazanavetsLearn everything there is to learn about SignalR and how to integrate it with the latest .NET 6 and C# 10 features. Learn how to connect any type of client to SignalR, including plain WebSocket client. Learn how to build interactive applications that can communicate with each other in real time without making excessive calls.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
The easiest way to learn design patterns
Fiodar SazanavetsLearn design patterns in the easiest way possible. You will no longer have to brute-force your way through each one of them while trying to figure out how it works. The book provides a unique methodology that will make your understanding of design patterns stick. It can also be used as a reference book where you can find design patterns in seconds.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
CCIE Service Provider Version 4 Written and Lab Exam Comprehensive Guide
Nicholas RussoThe service provider landscape has changed rapidly over the past several years. Networking vendors are continuing to propose new standards, techniques, and procedures for overcoming new challenges while concurrently reducing costs and delivering new services. Cisco has recently updated the CCIE Service Provider track to reflect these changes; this book represents the author's personal journey in achieving that certification.
Cronache di Domain-Driven Design
Francesco Strazzullo, Matteo Baglini, Gianluca Padovani, Emanuele DelBono, Marco Consolaro, Alessandro Colla, Uberto Barbini, Alberto Acerbis, Julie Camosseto, DDD Open, and Alberto BrandoliniCronache di Domain-Driven Design: un libro corale in italiano fatto di storie indipendenti tra loro, che sono il risultato dell’applicazione di Domain-Driven Design su progetti reali.
Functional event-driven architecture: Powered by Scala 3
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way, mainly powered by Fs2 streams in Scala 3!
Leverage your functional programming skills by designing and writing stateless microservices that scale, powered by stateful message brokers.
Top Bundles
- #1
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #2
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #3
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #4
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible...