Leanpub Header

Skip to main content

C++17 - The Complete Guide

All new language and library features of C++17 (for those who know previous versions).

Learn how C++17 impacts day-to-day C++ programming, how to benefit in practice, how to combine new features, and how to avoid all new traps.

Hardcover version (please, prefer a local bookstore)

Paperback version

Spanish version

C++23

C++20

Bundle with C++ Move Semantics

Minimum price

$19.90

$39.90

You pay

$39.90

Author earns

$31.92
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
4,645
Readers
0
Pages
90,926Words
About

About

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

Share this book

Categories

Author

About the Author

Nicolai M. Josuttis

Nicolai Josuttis (http://www.josuttis.com) is well known in the programming community because he not only speaks and writes with authority, being the (co-)author of the world-wide best sellers

but is also an innovative presenter, having talked at various conferences and events.

He is an independent trainer and speaker being active in C++ standardization for more than 20 years.

Leanpub Podcast

Episode 89

An Interview with Nicolai M. Josuttis

Contents

Table of Contents

Preface

  1. Versions of This Book
  2. Acknowledgments

About This Book

  1. What You Should Know Before Reading This Book
  2. Overall Structure of the Book
  3. How to Read This Book
  4. Error Terminology
  5. The C++17 Standard
  6. Example Code and Additional Information
  7. Feedback
  8. IBasic Language Features

1.Structured Bindings

  1. 1.1Structured Bindings in Detail
  2. 1.2Where Structured Bindings Can Be Used
  3. 1.2.1Structures and Classes
  4. 1.2.2Raw Arrays
  5. 1.2.3std::pair, std::tuple, and std::array
  6. 1.3Providing a Tuple-Like API for Structured Bindings
  7. 1.4Afternotes

2.if and switch with Initialization

  1. 2.1if with Initialization
  2. 2.2switch with Initialization
  3. 2.3Afternotes

3.Inline Variables

  1. 3.1Motivation for Inline Variables
  2. 3.2Using Inline Variables
  3. 3.3constexpr Now Implies inline For Static Members
  4. 3.4Inline Variables and thread_local
  5. 3.5Afternotes

4.Aggregate Extensions

  1. 4.1Motivation for Extended Aggregate Initialization
  2. 4.2Using Extended Aggregate Initialization
  3. 4.3Definition of Aggregates
  4. 4.4Backward Incompatibilities
  5. 4.5Afternotes

5.Mandatory Copy Elision or Passing Unmaterialized Objects

  1. 5.1Motivation for Mandatory Copy Elision for Temporaries
  2. 5.2Benefit of Mandatory Copy Elision for Temporaries
  3. 5.3Clarified Value Categories
  4. 5.3.1Value Categories
  5. 5.3.2Value Categories Since C++17
  6. 5.4Unmaterialized Return Value Passing
  7. 5.5Afternotes

6.Lambda Extensions

  1. 6.1constexpr Lambdas
  2. 6.1.1Using constexpr Lambdas
  3. 6.2Passing Copies of this to Lambdas
  4. 6.3Capturing by const Reference
  5. 6.4Afternotes

7.New Attributes and Attribute Features

  1. 7.1Attribute [[nodiscard]]
  2. 7.2Attribute [[maybe_unused]]
  3. 7.3Attribute [[fallthrough]]
  4. 7.4General Attribute Extensions
  5. 7.5Afternotes

8.Other Language Features

  1. 8.1Nested Namespaces
  2. 8.2Defined Expression Evaluation Order
  3. 8.3Relaxed Enum Initialization from Integral Values
  4. 8.4Fixed Direct List Initialization with auto
  5. 8.5Hexadecimal Floating-Point Literals
  6. 8.6UTF-8 Character Literals
  7. 8.7Exception Specifications as Part of the Type
  8. 8.8Single-Argument static_assert
  9. 8.9Preprocessor Condition __has_include
  10. 8.10Afternotes
  11. IITemplate Features

9.Class Template Argument Deduction

  1. 9.1Use of Class Template Argument Deduction
  2. 9.1.1Copying by Default
  3. 9.1.2Deducing the Type of Lambdas
  4. 9.1.3No Partial Class Template Argument Deduction
  5. 9.1.4Class Template Argument Deduction Instead of Convenience Functions
  6. 9.2Deduction Guides
  7. 9.2.1Using Deduction Guides to Force Decay
  8. 9.2.2Non-Template Deduction Guides
  9. 9.2.3Deduction Guides versus Constructors
  10. 9.2.4Explicit Deduction Guides
  11. 9.2.5Deduction Guides for Aggregates
  12. 9.2.6Standard Deduction Guides
  13. 9.3Afternotes

10.Compile-Time if

  1. 10.1Motivation for Compile-Time if
  2. 10.2Using Compile-Time if
  3. 10.2.1Caveats for Compile-Time if
  4. 10.2.2Other Compile-Time if Examples
  5. 10.3Compile-Time if with Initialization
  6. 10.4Using Compile-Time if Outside Templates
  7. 10.5Afternotes

11.Fold Expressions

  1. 11.1Motivation for Fold Expressions
  2. 11.2Using Fold Expressions
  3. 11.2.1Dealing with Empty Parameter Packs
  4. 11.2.2Supported Operators
  5. 11.2.3Using Fold Expressions for Types
  6. 11.3Afternotes

12.Dealing with String Literals as Template Parameters

  1. 12.1Using Strings in Templates
  2. 12.2Afternotes

13.Placeholder Types like auto as Template Parameters

  1. 13.1Using auto for Template Parameters
  2. 13.1.1Parameterizing Templates for Characters and Strings
  3. 13.1.2Defining Metaprogramming Constants
  4. 13.2Using auto as Variable Template Parameter
  5. 13.3Using decltype(auto) as Template Parameter
  6. 13.4Afternotes

14.Extended Using Declarations

  1. 14.1Using Variadic Using Declarations
  2. 14.2Variadic Using Declarations for Inheriting Constructors
  3. 14.3Afternotes
  4. IIINew Library Components

15.std::optional<>

  1. 15.1Using std::optional<>
  2. 15.1.1Optional Return Values
  3. 15.1.2Optional Arguments and Data Members
  4. 15.2std::optional<> Types and Operations
  5. 15.2.1std::optional<> Types
  6. 15.2.2std::optional<> Operations
  7. 15.3Special Cases
  8. 15.3.1Optional of Boolean or Raw Pointer Values
  9. 15.3.2Optional of Optional
  10. 15.4Afternotes

16.std::variant<>

  1. 16.1Motivation for std::variant<>
  2. 16.2Using std::variant<>
  3. 16.3std::variant<> Types and Operations
  4. 16.3.1std::variant<> Types
  5. 16.3.2std::variant<> Operations
  6. 16.3.3Visitors
  7. 16.3.4Valueless by Exception
  8. 16.4Polymorphism and Heterogeneous Collections with std::variant
  9. 16.4.1Geometric Objects with std::variant
  10. 16.4.2Other Heterogeneous Collections with std::variant
  11. 16.4.3Comparing variant Polymorphism
  12. 16.5Special Cases with std::variant<>
  13. 16.5.1Having Both bool and std::string Alternatives
  14. 16.6Afternotes

17.std::any

  1. 17.1Using std::any
  2. 17.2std::any Types and Operations
  3. 17.2.1Any Types
  4. 17.2.2Any Operations
  5. 17.3Afternotes

18.std::byte

  1. 18.1Using std::byte
  2. 18.2std::byte Types and Operations
  3. 18.2.1std::byte Types
  4. 18.2.2std::byte Operations
  5. 18.3Afternotes

19.String Views

  1. 19.1Differences Compared to std::string
  2. 19.2Using String Views
  3. 19.3Using String Views as Parameters
  4. 19.3.1String View Considered Harmful
  5. 19.4String View Types and Operations
  6. 19.4.1Concrete String View Types
  7. 19.4.2String View Operations
  8. 19.4.3String View Support by Other Types
  9. 19.5Using String Views in APIs
  10. 19.5.1Using String Views instead of Strings
  11. 19.6Afternotes

20.The Filesystem Library

  1. 20.1Basic Examples
  2. 20.1.1Print Attributes of a Passed Filesystem Path
  3. 20.1.2Switch Over Filesystem Types
  4. 20.1.3Create Different Types of Files {#createfiles.cpp}
  5. 20.1.4Dealing with Filesystems Using Parallel Algorithms
  6. 20.2Principles and Terminology
  7. 20.2.1General Portability Disclaimer
  8. 20.2.2Namespace
  9. 20.2.3Paths
  10. 20.2.4Normalization
  11. 20.2.5Member Function versus Free-Standing Functions
  12. 20.2.6Error Handling
  13. 20.2.7File Types
  14. 20.3Path Operations
  15. 20.3.1Path Creation
  16. 20.3.2Path Inspection
  17. 20.3.3Path I/O and Conversions
  18. 20.3.4Conversions Between Native and Generic Format
  19. 20.3.5Path Modifications
  20. 20.3.6Path Comparisons
  21. 20.3.7Other Path Operations
  22. 20.4Filesystem Operations
  23. 20.4.1File Attributes
  24. 20.4.2File Status
  25. 20.4.3Permissions
  26. 20.4.4Filesystem Modifications
  27. 20.4.5Symbolic Links and Filesystem-Dependent Path Conversions
  28. 20.4.6Other Filesystem Operations
  29. 20.5Iterating Over Directories
  30. 20.5.1Directory Entries
  31. 20.6Afternotes
  32. IVLibrary Extensions and Modifications

21.Extensions of Type Traits

  1. 21.1Type Traits Suffix _v
  2. 21.2New Type Traits
  3. 21.3Afternotes

22.Parallel STL Algorithms

  1. 22.1Using Parallel Algorithms
  2. 22.1.1Using a Parallel for_each()
  3. 22.1.2Using a Parallel sort()
  4. 22.2Execution Policies
  5. 22.3Exception Handling
  6. 22.4Benefit of Not Using Parallel Algorithms
  7. 22.5Overview of Parallel Algorithms
  8. 22.6Motivation for New Algorithms for Parallel Processing
  9. 22.6.1reduce()
  10. 22.7Afternotes

23.New STL Algorithms in Detail

  1. 23.1std::for_each_n()
  2. 23.2New Numeric STL Algorithms
  3. 23.2.1std::reduce()
  4. 23.2.2std::transform_reduce()
  5. 23.2.3std::inclusive_scan() and std::exclusive_scan()
  6. 23.2.4std::transform_inclusive_scan() and std::transform_exclusive_scan()
  7. 23.3Afternotes

24.Substring and Subsequence Searchers

  1. 24.1Using Substring Searchers
  2. 24.1.1Using Searchers with search()
  3. 24.1.2Using Searchers Directly
  4. 24.2Using General Subsequence Searchers
  5. 24.3Using Searcher Predicates
  6. 24.4Afternotes

25.Other Utility Functions and Algorithms

  1. 25.1size(), empty(), and data()
  2. 25.1.1Generic size() Function
  3. 25.1.2Generic empty() Function
  4. 25.1.3Generic data() Function
  5. 25.2as_const()
  6. 25.2.1Capturing by Const Reference
  7. 25.3clamp()
  8. 25.4sample()
  9. 25.5Afternotes

26.Container and String Extensions

  1. 26.1Node Handles
  2. 26.1.1Modifying a Key
  3. 26.1.2Moving Nodes Between Containers
  4. 26.1.3Merging Containers
  5. 26.2Emplace Improvements
  6. 26.2.1Return Type of Emplace Functions
  7. 26.2.2try_emplace() and insert_or_assign() for Maps
  8. 26.3Container Support for Incomplete Types
  9. 26.4String Improvements
  10. 26.5Afternotes

27.Multi-Threading and Concurrency

  1. 27.1Supplementary Mutexes and Locks
  2. 27.1.1std::scoped_lock
  3. 27.1.2std::shared_mutex
  4. 27.2is_always_lock_free for Atomics
  5. 27.3Cache Line Sizes
  6. 27.4Afternotes

28.Other Small Library Features and Modifications

  1. 28.1std::uncaught_exceptions()
  2. 28.2Shared Pointer Improvements
  3. 28.2.1Special handling for Shared Pointers to Raw C Arrays
  4. 28.2.2reinterpret_pointer_cast for Shared Pointers
  5. 28.2.3weak_type for Shared Pointers
  6. 28.2.4weak_from_this for Shared Pointers
  7. 28.3Numeric Extensions
  8. 28.3.1Greatest Common Divisor and Least Common Multiple
  9. 28.3.2Three-Argument Overloads of std::hypot()
  10. 28.3.3Mathematical Special Functions
  11. 28.4chrono Extensions
  12. 28.5constexpr Extensions and Fixes
  13. 28.6noexcept Extensions and Fixes
  14. 28.7Afternotes
  15. VExpert Utilities

29.Polymorphic Memory Resources (PMR)

  1. 29.1Using Standard Memory Resources
  2. 29.1.1Motivating Example
  3. 29.1.2Standard Memory Resources
  4. 29.1.3Standard Memory Resources in Detail
  5. 29.2Defining Custom Memory Resources
  6. 29.2.1Equality of Memory Resources
  7. 29.3Providing Memory Resource Support for Custom Types
  8. 29.3.1Definition of a PMR Type
  9. 29.3.2Using a PMR Type
  10. 29.3.3Dealing with the Different Types
  11. 29.4Afternotes

30.new and delete with Over-Aligned Data

  1. 30.1Using new with Alignments
  2. 30.1.1Distinct Dynamic/Heap Memory Arenas
  3. 30.1.2Passing the Alignment with the new Expression
  4. 30.2Implementing operator new() for Aligned Memory
  5. 30.2.1Implementing Aligned Allocation Before C++17
  6. 30.2.2Implementing Type-Specific operator new()
  7. 30.3Implementing Global operator new()
  8. 30.3.1Backward Incompatibilities
  9. 30.4Tracking All ::new Calls
  10. 30.5Afternotes

31.std::to_chars() and std::from_chars()

  1. 31.1Motivation for Low-Level Conversions between Character Sequences and Numeric Values
  2. 31.2Example Usage
  3. 31.2.1from_chars()
  4. 31.2.2to_chars()
  5. 31.3Floating-Point Round-Trip Support
  6. 31.4Afternotes

32.std::launder()

  1. 32.1Motivation for std::launder()
  2. 32.2How launder() Solves the Problem
  3. 32.3Why/When launder() Does Not Work
  4. 32.4Afternotes

33.Improvements for Implementing Generic Code

  1. 33.1std::invoke<>()
  2. 33.2std::bool_constant<>
  3. 33.3std::void_t<>
  4. 33.4Afternotes
  5. VIFinal General Hints

34.Common C++17 Settings

  1. 34.1Value of __cplusplus
  2. 34.2Compatibility to C11
  3. 34.3Dealing with Signal Handlers
  4. 34.4Forward Progress Guarantees
  5. 34.5Afternotes

35.Deprecated and Removed Features

  1. 35.1Deprecated and Removed Core Language Features
  2. 35.1.1Throw Specifications
  3. 35.1.2Keyword register
  4. 35.1.3Disable ++ for bool
  5. 35.1.4Trigraphs
  6. 35.1.5Definition/Redeclaration of static constexpr Members
  7. 35.2Deprecated and Removed Library Features
  8. 35.2.1auto_ptr
  9. 35.2.2Algorithm random_shuffle()
  10. 35.2.3unary_function and binary_function
  11. 35.2.4ptr_fun(), mem_fun(), and Binders
  12. 35.2.5Allocator Support for std::function<>
  13. 35.2.6Deprecated IOStream Aliases
  14. 35.2.7Deprecated Library Features
  15. 35.3Afternotes

Glossary

  1. B
  2. bitmask type
  3. F
  4. full specialization
  5. I
  6. incomplete type
  7. P
  8. partial specialization
  9. S
  10. small/short string optimization (SSO)
  11. V
  12. variable template
  13. variadic template

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

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) and EPUB (for phones, tablets and 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub