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
-
-
Preface
- Versions of This Book
- Acknowledgments
-
About This Book
- What You Should Know Before Reading This Book
- Overall Structure of the Book
- How to Read This Book
- Error Terminology
- The C++17 Standard
- Example Code and Additional Information
- Feedback
-
Preface
-
I Basic Language Features
-
1. Structured Bindings
- 1.1 Structured Bindings in Detail
-
1.2 Where Structured Bindings Can Be Used
- 1.2.1 Structures and Classes
- 1.2.2 Raw Arrays
-
1.2.3
std::pair
,std::tuple
, andstd::array
- 1.3 Providing a Tuple-Like API for Structured Bindings
- 1.4 Afternotes
-
2.
if
andswitch
with Initialization-
2.1
if
with Initialization -
2.2
switch
with Initialization - 2.3 Afternotes
-
2.1
-
3. Inline Variables
- 3.1 Motivation for Inline Variables
- 3.2 Using Inline Variables
-
3.3
constexpr
Now Impliesinline
For Static Members -
3.4 Inline Variables and
thread_local
- 3.5 Afternotes
-
4. Aggregate Extensions
- 4.1 Motivation for Extended Aggregate Initialization
- 4.2 Using Extended Aggregate Initialization
- 4.3 Definition of Aggregates
- 4.4 Backward Incompatibilities
- 4.5 Afternotes
-
5. Mandatory Copy Elision or Passing Unmaterialized Objects
- 5.1 Motivation for Mandatory Copy Elision for Temporaries
- 5.2 Benefit of Mandatory Copy Elision for Temporaries
-
5.3 Clarified Value Categories
- 5.3.1 Value Categories
- 5.3.2 Value Categories Since C++17
- 5.4 Unmaterialized Return Value Passing
- 5.5 Afternotes
-
6. Lambda Extensions
-
6.1
constexpr
Lambdas-
6.1.1 Using
constexpr
Lambdas
-
6.1.1 Using
-
6.2 Passing Copies of
this
to Lambdas -
6.3 Capturing by
const
Reference - 6.4 Afternotes
-
6.1
-
7. New Attributes and Attribute Features
-
7.1 Attribute
[[nodiscard]]
-
7.2 Attribute
[[maybe_unused]]
-
7.3 Attribute
[[fallthrough]]
- 7.4 General Attribute Extensions
- 7.5 Afternotes
-
7.1 Attribute
-
8. Other Language Features
- 8.1 Nested Namespaces
- 8.2 Defined Expression Evaluation Order
- 8.3 Relaxed Enum Initialization from Integral Values
-
8.4 Fixed Direct List Initialization with
auto
- 8.5 Hexadecimal Floating-Point Literals
- 8.6 UTF-8 Character Literals
- 8.7 Exception Specifications as Part of the Type
-
8.8 Single-Argument
static_assert
-
8.9 Preprocessor Condition
__has_include
- 8.10 Afternotes
-
1. Structured Bindings
-
II Template Features
-
9. Class Template Argument Deduction
-
9.1 Use of Class Template Argument Deduction
- 9.1.1 Copying by Default
- 9.1.2 Deducing the Type of Lambdas
- 9.1.3 No Partial Class Template Argument Deduction
- 9.1.4 Class Template Argument Deduction Instead of Convenience Functions
-
9.2 Deduction Guides
- 9.2.1 Using Deduction Guides to Force Decay
- 9.2.2 Non-Template Deduction Guides
- 9.2.3 Deduction Guides versus Constructors
- 9.2.4 Explicit Deduction Guides
- 9.2.5 Deduction Guides for Aggregates
- 9.2.6 Standard Deduction Guides
- 9.3 Afternotes
-
9.1 Use of Class Template Argument Deduction
-
10. Compile-Time
if
-
10.1 Motivation for Compile-Time
if
-
10.2 Using Compile-Time
if
-
10.2.1 Caveats for Compile-Time
if
-
10.2.2 Other Compile-Time
if
Examples
-
10.2.1 Caveats for Compile-Time
-
10.3 Compile-Time
if
with Initialization -
10.4 Using Compile-Time
if
Outside Templates - 10.5 Afternotes
-
10.1 Motivation for Compile-Time
-
11. Fold Expressions
- 11.1 Motivation for Fold Expressions
-
11.2 Using Fold Expressions
- 11.2.1 Dealing with Empty Parameter Packs
- 11.2.2 Supported Operators
- 11.2.3 Using Fold Expressions for Types
- 11.3 Afternotes
-
12. Dealing with String Literals as Template Parameters
- 12.1 Using Strings in Templates
- 12.2 Afternotes
-
13. Placeholder Types like
auto
as Template Parameters-
13.1 Using
auto
for Template Parameters- 13.1.1 Parameterizing Templates for Characters and Strings
- 13.1.2 Defining Metaprogramming Constants
-
13.2 Using
auto
as Variable Template Parameter -
13.3 Using
decltype(auto)
as Template Parameter - 13.4 Afternotes
-
13.1 Using
-
14. Extended Using Declarations
- 14.1 Using Variadic Using Declarations
- 14.2 Variadic Using Declarations for Inheriting Constructors
- 14.3 Afternotes
-
9. Class Template Argument Deduction
-
III New Library Components
-
15.
std::optional<>
-
15.1 Using
std::optional<>
- 15.1.1 Optional Return Values
- 15.1.2 Optional Arguments and Data Members
-
15.2
std::optional<>
Types and Operations-
15.2.1
std::optional<>
Types -
15.2.2
std::optional<>
Operations
-
15.2.1
-
15.3 Special Cases
- 15.3.1 Optional of Boolean or Raw Pointer Values
- 15.3.2 Optional of Optional
- 15.4 Afternotes
-
15.1 Using
-
16.
std::variant<>
-
16.1 Motivation for
std::variant<>
-
16.2 Using
std::variant<>
-
16.3
std::variant<>
Types and Operations-
16.3.1
std::variant<>
Types -
16.3.2
std::variant<>
Operations - 16.3.3 Visitors
- 16.3.4 Valueless by Exception
-
16.3.1
-
16.4 Polymorphism and Heterogeneous Collections with
std::variant
-
16.4.1 Geometric Objects with
std::variant
-
16.4.2 Other Heterogeneous Collections with
std::variant
-
16.4.3 Comparing
variant
Polymorphism
-
16.4.1 Geometric Objects with
-
16.5 Special Cases with
std::variant<>
-
16.5.1 Having Both
bool
andstd::string
Alternatives
-
16.5.1 Having Both
- 16.6 Afternotes
-
16.1 Motivation for
-
17.
std::any
-
17.1 Using
std::any
-
17.2
std::any
Types and Operations- 17.2.1 Any Types
- 17.2.2 Any Operations
- 17.3 Afternotes
-
17.1 Using
-
18.
std::byte
-
18.1 Using
std::byte
-
18.2
std::byte
Types and Operations-
18.2.1
std::byte
Types -
18.2.2
std::byte
Operations
-
18.2.1
- 18.3 Afternotes
-
18.1 Using
-
19. String Views
-
19.1 Differences Compared to
std::string
- 19.2 Using String Views
-
19.3 Using String Views as Parameters
- 19.3.1 String View Considered Harmful
-
19.4 String View Types and Operations
- 19.4.1 Concrete String View Types
- 19.4.2 String View Operations
- 19.4.3 String View Support by Other Types
-
19.5 Using String Views in APIs
- 19.5.1 Using String Views instead of Strings
- 19.6 Afternotes
-
19.1 Differences Compared to
-
20. The Filesystem Library
-
20.1 Basic Examples
- 20.1.1 Print Attributes of a Passed Filesystem Path
- 20.1.2 Switch Over Filesystem Types
- 20.1.3 Create Different Types of Files {#createfiles.cpp}
- 20.1.4 Dealing with Filesystems Using Parallel Algorithms
-
20.2 Principles and Terminology
- 20.2.1 General Portability Disclaimer
- 20.2.2 Namespace
- 20.2.3 Paths
- 20.2.4 Normalization
- 20.2.5 Member Function versus Free-Standing Functions
- 20.2.6 Error Handling
- 20.2.7 File Types
-
20.3 Path Operations
- 20.3.1 Path Creation
- 20.3.2 Path Inspection
- 20.3.3 Path I/O and Conversions
- 20.3.4 Conversions Between Native and Generic Format
- 20.3.5 Path Modifications
- 20.3.6 Path Comparisons
- 20.3.7 Other Path Operations
-
20.4 Filesystem Operations
- 20.4.1 File Attributes
- 20.4.2 File Status
- 20.4.3 Permissions
- 20.4.4 Filesystem Modifications
- 20.4.5 Symbolic Links and Filesystem-Dependent Path Conversions
- 20.4.6 Other Filesystem Operations
-
20.5 Iterating Over Directories
- 20.5.1 Directory Entries
- 20.6 Afternotes
-
20.1 Basic Examples
-
15.
-
IV Library Extensions and Modifications
-
21. Extensions of Type Traits
-
21.1 Type Traits Suffix
_v
- 21.2 New Type Traits
- 21.3 Afternotes
-
21.1 Type Traits Suffix
-
22. Parallel STL Algorithms
-
22.1 Using Parallel Algorithms
-
22.1.1 Using a Parallel
for_each()
-
22.1.2 Using a Parallel
sort()
-
22.1.1 Using a Parallel
- 22.2 Execution Policies
- 22.3 Exception Handling
- 22.4 Benefit of Not Using Parallel Algorithms
- 22.5 Overview of Parallel Algorithms
-
22.6 Motivation for New Algorithms for Parallel Processing
-
22.6.1
reduce()
-
22.6.1
- 22.7 Afternotes
-
22.1 Using Parallel Algorithms
-
23. New STL Algorithms in Detail
-
23.1
std::for_each_n()
-
23.2 New Numeric STL Algorithms
-
23.2.1
std::reduce()
-
23.2.2
std::transform_reduce()
-
23.2.3
std::inclusive_scan()
andstd::exclusive_scan()
-
23.2.4
std::transform_inclusive_scan()
andstd::transform_exclusive_scan()
-
23.2.1
- 23.3 Afternotes
-
23.1
-
24. Substring and Subsequence Searchers
-
24.1 Using Substring Searchers
-
24.1.1 Using Searchers with
search()
- 24.1.2 Using Searchers Directly
-
24.1.1 Using Searchers with
- 24.2 Using General Subsequence Searchers
- 24.3 Using Searcher Predicates
- 24.4 Afternotes
-
24.1 Using Substring Searchers
-
25. Other Utility Functions and Algorithms
-
25.1
size()
,empty()
, anddata()
-
25.1.1 Generic
size()
Function -
25.1.2 Generic
empty()
Function -
25.1.3 Generic
data()
Function
-
25.1.1 Generic
-
25.2
as_const()
- 25.2.1 Capturing by Const Reference
-
25.3
clamp()
-
25.4
sample()
- 25.5 Afternotes
-
25.1
-
26. Container and String Extensions
-
26.1 Node Handles
- 26.1.1 Modifying a Key
- 26.1.2 Moving Nodes Between Containers
- 26.1.3 Merging Containers
-
26.2 Emplace Improvements
- 26.2.1 Return Type of Emplace Functions
-
26.2.2
try_emplace()
andinsert_or_assign()
for Maps -
26.2.3
try_emplace()
-
26.2.4
insert_or_assign()
- 26.3 Container Support for Incomplete Types
- 26.4 String Improvements
- 26.5 Afternotes
-
26.1 Node Handles
-
27. Multi-Threading and Concurrency
-
27.1 Supplementary Mutexes and Locks
-
27.1.1
std::scoped_lock
-
27.1.2
std::shared_mutex
-
27.1.1
-
27.2
is_always_lock_free
for Atomics - 27.3 Cache Line Sizes
- 27.4 Afternotes
-
27.1 Supplementary Mutexes and Locks
-
28. Other Small Library Features and Modifications
-
28.1
std::uncaught_exceptions()
-
28.2 Shared Pointer Improvements
- 28.2.1 Special handling for Shared Pointers to Raw C Arrays
-
28.2.2
reinterpret_pointer_cast
for Shared Pointers -
28.2.3
weak_type
for Shared Pointers -
28.2.4
weak_from_this
for Shared Pointers
-
28.3 Numeric Extensions
- 28.3.1 Greatest Common Divisor and Least Common Multiple
-
28.3.2 Three-Argument Overloads of
std::hypot()
- 28.3.3 Mathematical Special Functions
-
28.4
chrono
Extensions -
28.5
constexpr
Extensions and Fixes -
28.6
noexcept
Extensions and Fixes - 28.7 Afternotes
-
28.1
-
21. Extensions of Type Traits
-
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 Resource Support for Custom Types
- 29.3.1 Definition of a PMR Type
- 29.3.2 Using 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.
std::to_chars()
andstd::from_chars()
- 31.1 Motivation for Low-Level Conversions between Character Sequences and Numeric Values
-
31.2 Example Usage
-
31.2.1
from_chars()
-
31.2.2
to_chars()
-
31.2.1
- 31.3 Floating-Point Round-Trip Support
- 31.4 Afternotes
-
32.
std::launder()
-
32.1 Motivation for
std::launder()
-
32.2 How
launder()
Solves the Problem -
32.3 Why/When
launder()
Does Not Work - 32.4 Afternotes
-
32.1 Motivation for
-
33. Improvements for Implementing Generic Code
-
33.1
std::invoke<>()
-
33.2
std::bool_constant<>
-
33.3
std::void_t<>
- 33.4 Afternotes
-
33.1
-
29. Polymorphic Memory Resources (PMR)
-
VI Final General Hints
-
34. Common C++17 Settings
-
34.1 Value of
__cplusplus
- 34.2 Compatibility to C11
- 34.3 Dealing with Signal Handlers
- 34.4 Forward Progress Guarantees
- 34.5 Afternotes
-
34.1 Value of
-
35. Deprecated and Removed Features
-
35.1 Deprecated and Removed Core Language Features
- 35.1.1 Throw Specifications
-
35.1.2 Keyword
register
-
35.1.3 Disable
++
forbool
- 35.1.4 Trigraphs
-
35.1.5 Definition/Redeclaration of
static
constexpr
Members
-
35.2 Deprecated and Removed Library Features
-
35.2.1
auto_ptr
-
35.2.2 Algorithm
random_shuffle()
-
35.2.3
unary_function
andbinary_function
-
35.2.4
ptr_fun()
,mem_fun()
, and Binders -
35.2.5 Allocator Support for
std::function<>
- 35.2.6 Deprecated IOStream Aliases
- 35.2.7 Deprecated Library Features
-
35.2.1
- 35.3 Afternotes
-
35.1 Deprecated and Removed Core Language Features
-
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
-
34. Common C++17 Settings
- Notes
Authors have earned$9,909,464writing, 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
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
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++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Continuous Delivery Pipelines
Dave FarleyThis practical handbook provides a step-by-step guide for you to get the best continuous delivery pipeline for your software.
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.
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
The book is almost daily updated. These incremental updates ease my interaction with the proofreaders.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
Introductory Statistics with Randomization and Simulation
Mine Cetinkaya-Rundel, Christopher Barr, OpenIntro, and David DiezA complete foundation for Statistics, also serving as a foundation for Data Science, that introduces inference using randomization and simulation while covering traditional methods.
Leanpub revenue supports OpenIntro, so we can provide free desk copies to teachers interested in using our books in the classroom.
More resources: openintro.org.
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.
Java OOP Done Right
Alan MellorObject Oriented Programming is still a great way to create clean, maintainable code. But only if you use it right.
This book gives you 25 years of OO best practice, ready to use.
You'll learn to design objects behaviour-first, use TDD to help, then confidently apply Design Patterns, SOLID principles and Refactoring to make clean, crafted code.
Introducing EventStorming
Alberto BrandoliniThe deepest tutorial and explanation about EventStorming, straight from the inventor.
Discrete Mathematics for Computer Science
Alexander Shen, Alexander S. Kulikov, Vladimir Podolskii, and Aleksandr GolovnevThis book supplements the DM for CS Specialization at Coursera and contains many interactive puzzles, autograded quizzes, and code snippets. They are intended to help you to discover important ideas in discrete mathematics on your own. By purchasing the book, you will get all updates of the book free of charge when they are released.
Top Bundles
- #1
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... - #2
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... - #3
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #4
Modern C++ by Nicolai Josuttis
2 Books
- #5
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #6
"The C++ Standard Library" and "Concurrency with Modern C++"
2 Books
Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle. The first book gives you the details you should know about the C++ standard library; the second one dives deeper into concurrency with modern C++. In sum, you get more than 600 pages full of modern C++ and about 250 source files presenting the standard library... - #7
Mastering Containers
2 Books
Docker and Kubernetes are taking the world by storm! These books will get you up-to-speed fast! Docker Deep Dive is over 400 pages long, and covers all objectives on the Docker Certified Associate exam.The Kubernetes Book includes everything you need to get up and running with Kubernetes! - #8
Modern Management Made Easy
3 Books
Read all three Modern Management Made Easy books. Learn to manage yourself, lead and serve others, and lead the organization. - #9
The Future of Digital Health
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 and how technology giants such as Amazon or Google want to conquer... - #10
Django for Beginners/APIs/Professionals
3 Books