C++20 - The Complete Guide
$29.90
Minimum price
$49.90
Suggested price

C++20 - The Complete Guide

About the Book

C++20 is the next evolution in modern C++ programming, which will be supported step-by-step by the latest version of gcc, clang, and Visual C++.

C++20 is a big step, maybe even larger than C++11.

It contains a couple of new key features (modules, concepts, ranges, corountines) plus several small but 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++20. 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 C++20 in practice.

Testimonials:

"C++20 scared me for a few years, and I am a C++ educator. After reading Nico's fantastic new book, I may still be afraid of C++20, but at least now I have a much deeper understanding of what it actually is I am afraid of. Leor Zolman

"I use this book as reference almost everyday." Selvakumar Jawahar

Buy early, pay less, free updates

Note that this book is published step-by-step. The first public version was published in June 2021. Since then, the contents grows with new chapters, examples, and caveats about the features of C++20 and I integrate all feedback I get for the pages already published.

The book is feature complete now.

Just minore details and copy editing is missing.

See cppstd20.com for a detailed list of all the topics covered.

As written, once you bought the ebook 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

#cpp20tcg

About the Author

Nicolai M. Josuttis
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.

Bundles that include this book

$109.60
Bought separately
$59.00
Minimum price
$89.00
Suggested price

Table of Contents

  • Preface
    • An Experiment
    • Acknowledgments
    • Versions of This Book
  • About This Book
    • What You Should Know Before Reading This Book
    • Overall Structure of the Book
    • How to Read This Book
    • The Way I Implement
      • Initializations
      • Error Terminology
      • Code Simplifications
    • The C++ Standards
    • Example Code and Additional Information
    • Feedback
  • 1. Comparisons and Operator <=>
    • 1.1 Motivation for Operator<=>
      • 1.1.1 Defining Comparison Operators Before C++20
      • 1.1.2 Defining Comparison Operators Since C++20
    • 1.2 Defining and Using Comparisons
      • 1.2.1 Using Operator<=>
      • 1.2.2 Using Comparison Categories with operator<=>
      • 1.2.3 Calling Operator <=> Directly
      • 1.2.4 Dealing with Multiple Ordering Criteria
    • 1.3 Defining operator<=> and operator==
      • 1.3.1 Defaulted operator== and operator<=>
      • 1.3.2 Defaulted operator<=> Implies Defaulted operator==
      • 1.3.3 Implementation of the Defaulted operator<=>
    • 1.4 Overload Resolution with Rewritten Expressions
    • 1.5 Using Operator <=> in Generic Code
      • 1.5.1 Algorithm lexicographical_compare_three_way()
    • 1.6 Compatibility Issues with the Comparison Operators
      • 1.6.1 Delegating Free-Standing Comparison Operators
      • 1.6.2 Inheritance with Protected Members
    • 1.7 Afternotes
  • 2. Placeholder Types for Function Parameters
    • 2.1 auto for Parameters of Ordinary Functions
      • 2.1.1 auto for Parameters of Member Functions
    • 2.2 Using auto for Parameters in Practice
      • 2.2.1 Deferred Type Checks with auto
    • 2.3 auto for Parameters in Detail
      • 2.3.1 Basic Constraints for auto Parameters
      • 2.3.2 Combining Template and auto Parameters
    • 2.4 Afternotes
  • 3. Concepts, Requirements, and Constraints
    • 3.1 Motivating Example of Concepts and Requirements
      • 3.1.1 Improving the Template Step by Step
      • 3.1.2 A Complete Example with Concepts
      • 3.1.3 Constraining Alias Templates
      • 3.1.4 Constraining Variable Templates
      • 3.1.5 Constraining Member Functions
    • 3.2 Typical Applications of Concepts and Constraints in Practice
      • 3.2.1 Using Concepts to Understand Code and Error Messages
      • 3.2.2 Using Concepts to Disable Generic Code
      • 3.2.3 Using Requirements to Call Different Functions
      • 3.2.4 The Example as a Whole
      • 3.2.5 Former Workarounds
    • 3.3 Semantic Constraints
      • 3.3.1 Examples of Semantic Constraints
    • 3.4 Design Guidelines for Concepts
      • 3.4.1 Concepts Should Group Requirements
      • 3.4.2 Define Concepts with Care
    • 3.5 Afternotes
  • 4. Concepts, Requirements, and Constraints in Detail
    • 4.1 requires Clauses
      • 4.1.1 Using && and || in requires Clauses
    • 4.2 Ad-hoc Boolean Expressions
    • 4.3 requires Expressions
      • 4.3.1 Simple Requirements
      • 4.3.2 Type Requirements
      • 4.3.3 Compound Requirements
    • 4.4 Concepts in Detail
      • 4.4.1 Special Abilities of Concepts
      • 4.4.2 Concepts for Non-Type Template Parameters
    • 4.5 Subsuming Constraints with Concepts
      • 4.5.1 Indirect Subsumptions
      • 4.5.2 Defining Commutative Concepts
  • 5. Standard Concepts in Detail
    • 5.1 Overview of All Standard Concepts
      • 5.1.1 Header Files and Namespaces
      • 5.1.2 Standard Concepts Subsume
    • 5.2 Language-Related Concepts
      • 5.2.1 Arithmetic Concepts
      • 5.2.2 Object Concepts
      • 5.2.3 Concepts for Relationships between Types
      • 5.2.4 Comparison Concepts
    • 5.3 Concepts for Iterators and Ranges
      • 5.3.1 Concepts for Pointer-Like Objects
      • 5.3.2 Concepts for Iterators
      • 5.3.3 Iterator Concepts for Algorithms
    • 5.4 Concepts for Callables
      • 5.4.1 Basic Concepts for Callables
      • 5.4.2 Concepts for Callables Used by Iterators
    • 5.5 Auxiliary Concepts
      • 5.5.1 Concepts for Specific Type Attributes
      • 5.5.2 Concepts for Incrementable Types
  • 6. Ranges and Views
    •  
      • 6.0.1 Constraints and Utilities for Ranges
      • 6.0.2 Range Definitions with Sentinels and Counts
      • 6.0.3 Projections
      • 6.0.4 Utilities for Implementing Code for Ranges
      • 6.0.5 Limitations and Drawbacks of Ranges
    • 6.1 Borrowed Iterators and Ranges
      • 6.1.1 Borrowed Ranges
    • 6.2 Using Views
      • 6.2.1 Views on Ranges
    • 6.3 Views on Ranges That Are Destroyed or Modified
      • 6.3.1 Views with Write Access
      • 6.3.2 Views on Ranges That Change
      • 6.3.3 Copying Views Might Change Behavior
    • 6.4 Views and const
    • 6.5 Summary of All Container Idioms Broken By Views
    • 6.6 Afternotes
  • 7. Utilities for Ranges and Views
    • 7.1 Key Utilities for Using Ranges as Views
      • 7.1.1 std::views::all()
      • 7.1.2 std::views::counted()
    • 7.2 New Iterator and Sentinel Types
      • 7.2.1 std::counted_iterator
      • 7.2.2 std::common_iterator
      • 7.2.3 std::default_sentinel
      • 7.2.4 std::unreachable_sentinel
    • 7.3 New Functions for Dealing with Ranges
      • 7.3.1 Functions for Dealing with the Elements of Ranges (and Arrays)
      • 7.3.2 Functions for Dealing with Iterators
      • 7.3.3 Functions for Swapping and Moving Elements/Values
      • 7.3.4 Functions for Comparisons of Values
    • 7.4 New Type Functions/Utilities for Dealing with Ranges
      • 7.4.1 Generic Types of Ranges
      • 7.4.2 New Functional Types
      • 7.4.3 Other New Types for Dealing with Iterators
    • 7.5 Range Algorithms
      • 7.5.1 Algorithm Overview
  • 8. View Types in Detail
    • 8.1 Overview of All Views
      • 8.1.1 Overview of Wrapping and Generating Views
      • 8.1.2 Overview of Adapting Views
    • 8.2 Base Class and Namespace of Views
      • 8.2.1 Why Range Adaptors/Factories Have Their Own Namespace
    • 8.3 Source Views to External Elements
      • 8.3.1 Ref View
      • 8.3.2 Owning View
      • 8.3.3 Common View
    • 8.4 Generating Views
      • 8.4.1 Single View
      • 8.4.2 Empty View
      • 8.4.3 IStream View
      • 8.4.4 String View
      • 8.4.5 Span
    • 8.5 Filtering Views
      • 8.5.1 Take-While View
      • 8.5.2 Drop View
      • 8.5.3 Drop-While View
      • 8.5.4 Filter View
    • 8.6 Transforming Views
      • 8.6.1 Elements View
      • 8.6.2 Keys and Values View
    • 8.7 Mutating Views
    • 8.8 Views for Multiple Ranges
      • 8.8.1 Join View
  • 9. Spans
    • 9.1 Using Spans
      • 9.1.1 Example Using a Span with a Dynamic Extent
      • 9.1.2 Example Using a Span with Non-const Elements
      • 9.1.3 Example Using a Span with Fixed Extent
      • 9.1.4 Fixed vs. Dynamic Extent
    • 9.2 Spans Considered Harmful
    • 9.3 Design Aspects of Spans
      • 9.3.1 Lifetime Dependencies of Spans
      • 9.3.2 Performance of Spans
      • 9.3.3 const Correctness of Spans
      • 9.3.4 Using Spans as Parameters in Generic Code
    • 9.4 Span Operations
      • 9.4.1 Span Operations and Member Types Overview
      • 9.4.2 Constructors
      • 9.4.3 Operations for Sub-Spans
    • 9.5 Afternotes
  • 10. Formatted Output
    • 10.1 Formatted Output by Example
      • 10.1.1 Using std::format()
      • 10.1.2 Using std::format_to_n()
      • 10.1.3 Using std::format_to()
      • 10.1.4 Using std::formatted_size()
    • 10.2 Performance of the Formatting Library
      • 10.2.1 Using std::vformat() and vformat_to()
    • 10.3 Formatted Output in Detail
      • 10.3.1 General Format of Format Strings
      • 10.3.2 Standard Format Specifiers
      • 10.3.3 Width, Precision, and Fill Characters
    • 10.4 Internationalization
    • 10.5 Error Handling
    • 10.6 User-Defined Formatted Output
      • 10.6.1 Basic Formatter API
      • 10.6.2 Improved Parsing
      • 10.6.3 Using Standard Formatters for User-Defined Formatters
      • 10.6.4 Using Standard Formatters for Strings
    • 10.7 Afternotes
  • 11. Dates and Timezones for <chrono>
    • 11.1 Overview by Example
      • 11.1.1 Scheduling a Meeting on the 5th of Every Month
      • 11.1.2 Scheduling a Meeting on the Last Day of Every Month
      • 11.1.3 Scheduling a Meeting Every First Monday
      • 11.1.4 Using Different Timezones
    • 11.2 Basic Chrono Concepts and Terminology
    • 11.3 Basic Chrono Extensions with C++20
      • 11.3.1 Duration Types
      • 11.3.2 Timepoint Types
      • 11.3.3 Time Type hh_mm_ss
      • 11.3.4 Hours Utilities
    • 11.4 I/O with Chrono Types
      • 11.4.1 Formatted Input
    • 11.5 Using the Chrono Extensions in Practice
      • 11.5.1 Dealing with months and years
      • 11.5.2 Parsing Timepoints and Durations
    • 11.6 Timezones
      • 11.6.1 Characteristics of Timezones
      • 11.6.2 The IANA Timezone Database
      • 11.6.3 Using Timezones
      • 11.6.4 Custom Timezones
    • 11.7 Clocks in Detail
      • 11.7.1 The Pseudo Clock local_t
      • 11.7.2 Dealing with Leap Seconds
      • 11.7.3 Conversions between Clocks
    • 11.8 Other New Chrono Features
    • 11.9 Afternotes
  • 12. std::jthread and Stop Tokens
    • 12.1 Motivation for std::jthread
      • 12.1.1 The Problem of std::thread
      • 12.1.2 Using std::jthread
      • 12.1.3 Stop Sources and Stop Tokens in Detail
      • 12.1.4 Using Stop Callbacks
      • 12.1.5 Constraints and Guarantees of Stop Tokens
    • 12.2 std::jthread in Detail
      • 12.2.1 Using Stop Tokens with std::jthread
    • 12.3 Afternotes
  • 13. Concurrency Features
    • 13.1 Thread Synchronization with Latches and Barriers
      • 13.1.1 Latches
      • 13.1.2 Barriers
    • 13.2 Semaphores
      • 13.2.1 Example of Using Counting Semaphores
    • 13.3 Extensions for Atomic Types
      • 13.3.1 Atomic Shared Pointers
      • 13.3.2 Atomic Floating-Point Types
      • 13.3.3 Thread Synchronization with Atomic Types
      • 13.3.4 Extensions for std::atomic_flag
    • 13.4 Synchronized Output Streams
      • 13.4.1 Motivation for Synchronized Output Streams
      • 13.4.2 Using Synchronized Output Streams
      • 13.4.3 Using Synchronized Output Streams for Files
      • 13.4.4 Using Synchronized Output Streams as Output Streams
      • 13.4.5 Synchronized Output Streams in Practice
    • 13.5 Afternotes
  • 14. Coroutines
    • 14.1 What Are Coroutines?
    • 14.2 A First Coroutine Example
      • 14.2.1 Using the Coroutine
      • 14.2.2 Coroutines Calling Coroutines
      • 14.2.3 Bootstrapping Interface, Handle, and Promise
      • 14.2.4 Memory Management
    • 14.3 Coroutines That Yield or Return Values
      • 14.3.1 Using co_yield
    • 14.4 Coroutine Awaitables and Awaiters
      • 14.4.1 Passing Values From Suspension Back to the Coroutine
    • 14.5 Afternotes
  • 15. Coroutines in Detail
    • 15.1 Coroutine Constraints
    • 15.2 The Coroutine Frame and the Promises
      • 15.2.1 How Coroutine Interfaces, Promises, and Awaitables Interact
    • 15.3 Coroutine Promises in Detail
      • 15.3.1 Mandatory Promise Operations
      • 15.3.2 Promise Operations to Return or Yield Values
      • 15.3.3 Optional Promise Operations
    • 15.4 Coroutine Handles in Detail
      • 15.4.1 std::coroutine_handle<void>
    • 15.5 Exceptions in Coroutines
    • 15.6 Allocating Memory for the Coroutine Frame
      • 15.6.1 How Coroutines Allocate Memory
      • 15.6.2 Avoiding Heap Memory Allocation
      • 15.6.3 get_return_object_on_allocation_failure()
    • 15.7 co_await and Awaiters in Detail
      • 15.7.1 Details of the Awaiter Interface
      • 15.7.2 Letting co_await Update Running Coroutines
    • 15.8 Other Ways of Dealing with co_await
    • 15.9 Concurrent Use of Coroutines
      • 15.9.1 A Thread Pool for Coroutine Tasks
      • 15.9.2 What C++ Libraries Will Provide After C++20
  • 16. Modules
    • 16.1 Motivation for Modules Using a First Example
      • 16.1.1 Compiling Module Units
      • 16.1.2 Importing and Using a Module
    • 16.2 Modules with Multiple Files
      • 16.2.1 Module Units
      • 16.2.2 Interface Partitions
      • 16.2.3 Summary of Splitting Modules into Different Files
    • 16.3 Dealing with Modules in Practice
      • 16.3.1 Dealing with Header Files
    • 16.4 Modules in Detail
      • 16.4.1 Module Declaration and Export in Detail
      • 16.4.2 Umbrella Modules
      • 16.4.3 Module Import in Detail
    • 16.5 Afternotes
  • 17. Lambda Extensions
    •  
      • 17.0.1 Using Template Parameters for Generic Lambdas in Practice
      • 17.0.2 Explicit Specification of Lambda Template Parameters
    • 17.1 Calling the Default Constructor of Lambdas
    • 17.2 Lambdas as Non-Type Template Parameters
    • 17.3 consteval Lambdas
    • 17.4 Changes for Capturing
      • 17.4.1 Capturing Structured Bindings
      • 17.4.2 Capturing Parameter Packs of Variadic Templates
      • 17.4.3 Lambdas as Coroutines
    • 17.5 Afternotes
  • 18. Compile-Time Computing
    •  
      • 18.0.1 Using constinit in Practice
      • 18.0.2 How constinit Solves the Static Initialization Order Fiasco
    • 18.1 Keyword consteval
      • 18.1.1 A First consteval Example
      • 18.1.2 constexpr versus consteval
      • 18.1.3 Using consteval in Practice
      • 18.1.4 Compile-Time Value versus Compile-Time Context
      • 18.1.5 std::is_constant_evaluated() in Detail
    • 18.2 Using Heap Memory, Vectors, and Strings at Compile Time
      • 18.2.1 Using Vectors at Compile Time
      • 18.2.2 Returning a Collection at Compile Time
      • 18.2.3 Using Strings at Compile Time
    • 18.3 Other constexpr Extensions
      • 18.3.1 constexpr Language Extensions
      • 18.3.2 constexpr Library Extensions
    • 18.4 Afternotes
  • 19. Non-Type Template Parameter (NTTP) Extensions
    •  
      • 19.0.1 Floating-Point Values as Non-Type Template Parameters
      • 19.0.2 Lambdas as Non-Type Template Parameters
    • 19.1 Afternotes
  • 20. New Type Traits
    • 20.1 New Type Traits for Type Classification
      • 20.1.1 is_bounded_array_v<> and is_unbounded_array_v
    • 20.2 New Type Traits for Type Inspection
    • 20.3 New Type Traits for Type Conversion
      • 20.3.1 unwrap_reference<> and unwrap_ref_decay_t
      • 20.3.2 common_reference<>_t
      • 20.3.3 type_identity_t<>
    • 20.4 New Type Traits for Iterators
      • 20.4.1 iter_value_t<>
      • 20.4.2 iter_reference_t<> and iter_rvalue_reference_t<>
    • 20.5 Type Traits and Functions for Layout Compatibility
      • 20.5.1 is_layout_compatible_v<>
      • 20.5.2 is_pointer_interconvertible_base_of_v<>
      • 20.5.3 is_corresponding_member()
      • 20.5.4 is_pointer_interconvertible_with_class()
    • 20.6 Afternotes
  • 21. Small Improvements for the Core Language
    • 21.1 using for Enumeration Values
    • 21.2 Delegating Enumeration Types to Different Scopes
    • 21.3 New Character Type char8_t
      • 21.3.1 Changes in the C++ Standard Library for char8_t
    • 21.4 Improvements for Aggregates
      • 21.4.1 Designated Initializers
      • 21.4.2 Aggregate Initialization with Parentheses
    • 21.5 New Attributes and Attribute Features
      • 21.5.1 Attributes [[likely]] and [[unlikely]]
      • 21.5.2 Attribute [[nodiscard]] with Parameter
    • 21.6 Feature Test Macros
    • 21.7 Afternotes
  • 22. Small Improvements for Generic Programming
    •  
      • 22.0.1 Rules for Implicit typename in Detail
    • 22.1 Improvements for Aggregates in Generic Code
      • 22.1.1 Class Template Argument Deduction (CTAD) for Aggregates
    • 22.2 Conditional explicit
    • 22.3 Afternotes
  • 23. Small Improvements for the C++ Standard Library
    • 23.1 Updates for String Types
      • 23.1.1 String Members starts_with() and ends_with()
      • 23.1.2 Restricted String Member reserve()
    • 23.2 std::source_location
    • 23.3 Safe Comparisons of Integral Values and Sizes
      • 23.3.1 Safe Comparisons of Integral Values
    • 23.4 Utilities for Dealing with Bits
      • 23.4.1 Bit Operations
      • 23.4.2 std::bit_cast<>()
      • 23.4.3 std::endian
    • 23.5 <version>
    • 23.6 Extensions for Algorithms
      • 23.6.1 Range Support
      • 23.6.2 New Algorithms
    • 23.7 Afternotes
  • 24. Deprecated and Removed Features
    • 24.1 Deprecated and Removed Core Language Features
    • 24.2 Deprecated and Removed Library Features
      • 24.2.1 Removed Library Features
    • 24.3 Afternotes
  • Glossary
    • A
      • aggregate
      • argument-dependent lookup (ADL)
    • C
      • class template argument deduction (CTAD)
    • F
      • forwarding reference
      • full specialization
      • function object (functor)
    • G
      • glvalue
    • I
      • incomplete type
    • L
      • lvalue
    • P
      • partial specialization
      • predicate
      • prvalue
    • R
      • resource acquisition is initialization (RAII)
      • regular type
      • rvalue
    • S
      • semiregular type
      • substitution failure is not an error (SFINAE)
      • small string optimization (SSO)
      • stateless
      • standard template library (STL)
    • U
      • universal reference
    • V
      • value category
      • variable template
      • variadic template
    • X
      • xvalue
  • 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.

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...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

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

In fact, authors have earnedover $13 millionwriting, 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