Leanpub Header

Skip to main content

C++20

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

This book is available in multiple packages!

Pick Your Package
PDF
EPUB
WEB
1,416
Readers
677
Pages
About

About

About the Book

My book C++20 is a tutorial and a reference for the C++20 standard. It teaches you C++20 and provides details of this new, thrilling C++ standard. The exciting factor is mainly due to the big four of C++20.

  • Concepts change the way we think and program templates. They are semantic categories for the template parameters and enable you to express your intention directly in the type system. If something goes wrong, you get a clear error message.
  • The new ranges library enables it to perform algorithms directly on the container, compose the algorithm with the pipe symbol, and apply them to infinite data streams.
  • Thanks to coroutines, asynchronous programming in C++ has become mainstream. Coroutines are the base for cooperative tasks, event loops, infinite data streams, or pipelines.
  • Modules overcome the restrictions of header files. They promise a lot. For example, the separation of header and source files becomes obsolete as the preprocessor. Ultimately, we have faster built time and an easier way to build packages.

Share this book

Categories

Installments completed

3 / 3

Packages

Pick Your Package

All packages include the ebook in the following formats: PDF, EPUB, and Web

The Book

Minimum price

Suggested price$41.00

$33.00

    C++20 Team Edition: Five Copies

    Minimum price

    Suggested price$123.00

    Get five copies to the price of three. This package includes all code examples.

    $99.00

      Author

      About the Author

      Rainer Grimm

      I've worked as a software architect, team lead, and instructor since 1999. In 2002, I created a further education round at my company. I have given training courses since 2002. My first tutorials were about proprietary management software, but soon after, I began teaching Python and C++. In my spare time, I like to write articles about C++, Python, and Haskell. I also like to speak at conferences. I publish weekly on my English blog https://www.modernescpp.com.

      Since 2016, I have been an independent instructor giving seminars about modern C++ and Python. I have published several books in various languages about modern C++ and in particular, concurrency. Due to my profession, I always search for the best way to teach modern C++.

      My books "C++ 11 für Programmierer ", "C++" and "C++ Standardbibliothek kurz & gut" for the "kurz & gut" series were published by Pearson and O'Reilly. They are available in German, English, Korean, and Persian. In summer 2018 I published a new book on Leanpub: "Concurrency with Modern C++". This book is also available in German: "Modernes C++: Concurrency meistern".

      Contents

      Table of Contents

      Reader Testimonials

      Introduction

      1. Conventions
      2. Special Fonts
      3. Special Boxes
      4. Tip Headline
      5. Warning Headline
      6. Distilled Information
      7. Source Code
      8. Compilation of the Programs
      9. How should you read the Book?
      10. Personal Notes
      11. Acknowledgments
      12. About Me
      13. IAbout C++

      1.Historical Context

      1. 1.1C++98
      2. 1.2C++03
      3. 1.3TR1
      4. 1.4C++11
      5. 1.5C++14
      6. 1.6C++17

      2.Standardization

      1. 2.1Stage 3
      2. 2.2Stage 2
      3. 2.3Stage 1
      4. IIA Quick Overview of C++20

      3.C++20

      1. 3.1The Big Four
      2. 3.1.1Concepts
      3. 3.1.2Modules
      4. 3.1.3The Ranges Library
      5. 3.1.4Coroutines
      6. 3.2Core Language
      7. 3.2.1Three-Way Comparison Operator
      8. 3.2.2Designated Initialization
      9. 3.2.3consteval and constinit
      10. 3.2.4Template Improvements
      11. 3.2.5Lambda Improvements
      12. 3.2.6New Attributes
      13. 3.3The Standard Library
      14. 3.3.1std::span
      15. 3.3.2Container Improvements
      16. 3.3.3Arithmetic Utilities
      17. 3.3.4Formatting Library
      18. 3.3.5Calendar and Time Zones
      19. 3.4Concurrency
      20. 3.4.1Atomics
      21. 3.4.2Semaphores
      22. 3.4.3Latches and Barriers
      23. 3.4.4Cooperative Interruption
      24. 3.4.5std::jthread
      25. 3.4.6Synchronized Outputstreams
      26. IIIThe Details

      4.Core Language

      1. 4.1Concepts
      2. 4.1.1Two Wrong Approaches
      3. The Essence of Generic Programming
      4. 4.1.2Advantages of Concepts
      5. 4.1.3The long, long History
      6. 4.1.4Use of Concepts
      7. Avoid Compile-Time Predicates in Requires Clauses
      8. Test of Concepts
      9. 4.1.5Constrained and Unconstrained Placeholders
      10. 4.1.6Abbreviated Function Templates
      11. What we don’t get: Template Introduction
      12. 4.1.7Predefined Concepts
      13. 4.1.8Define Concepts
      14. Don’t define Concepts Recursively or try to Constrain them
      15. 4.1.9Requires Expressions
      16. 4.1.10User-Defined Concepts
      17. Regular Types
      18. Concepts in C++20: An Evolution or a Revolution?
      19. Distilled Information
      20. 4.2Modules
      21. 4.2.1A First Example
      22. 4.2.2Advantages
      23. 4.2.3The Details
      24. Compilation of a Module
      25. Common cl.exe Compiler Options
      26. Selectively Exporting Names in Namespaces
      27. Building the Executable with the Microsoft Compiler
      28. Compilation of the Module and its Submodules with the Microsoft Compiler
      29. Language Linkage
      30. Modules versus Precompiled Headers
      31. 4.2.4Further Aspects
      32. Distilled Information
      33. 4.3Equality Comparison and Three-Way Comparison
      34. 4.3.1Comparison before C++20
      35. 4.3.2Comparison since C++20
      36. Automatic Comparison of Pointers
      37. 4.3.3Comparison Categories
      38. 4.3.4Compiler-Generated Equality and Spaceship Operator
      39. Optimized == and != Operators
      40. 4.3.5Rewriting Expressions
      41. 4.3.6User-Defined and Auto-Generated Comparison Operators
      42. Similarity to Python
      43. Distilled Information
      44. 4.4Designated Initialization
      45. 4.4.1Aggregate Initialization
      46. 4.4.2Named Initialization of Class Members
      47. Differences Between C and C++
      48. Distilled Information
      49. 4.5consteval and constinit
      50. 4.5.1consteval
      51. 4.5.2constinit
      52. 4.5.3Comparison of const, constexpr, consteval, and constinit
      53. Initialization of a Local Non-Const Variable at Compile Time
      54. 4.5.4Solving the Static Initialization Order Fiasco
      55. Distilled Information
      56. 4.6Template Improvements
      57. 4.6.1Conditionally Explicit Constructor
      58. 4.6.2Non-Type Template Parameters (NTTP)
      59. Typical Non-Type Template Parameter
      60. Compile-Time Regular Expressions
      61. Distilled Information
      62. 4.7Lambda Improvements
      63. 4.7.1Template Parameter for Lambdas
      64. Class Template Argument Deduction
      65. 4.7.2Detection of the Implicit Copy of the this Pointer
      66. 4.7.3Lambdas in an Unevaluated Context and Stateless Lambdas can be Default-Constructed and Copy-Assigned
      67. 4.7.4consteval Lambdas
      68. Empty Parameter Clause
      69. 4.7.5Pack Expansion in Init-Capture
      70. Distilled Information
      71. 4.8New Attributes
      72. Attributes
      73. 4.8.1[[nodiscard("reason")]]
      74. The issue with std::async
      75. 4.8.2[[likely]] and [[unlikely]]
      76. 4.8.3[[no_unique_address]]
      77. Distilled Information
      78. 4.9Further Improvements
      79. 4.9.1volatile
      80. volatile and Multithreading Semantics
      81. 4.9.2Range-based for loop with Initializers
      82. 4.9.3Virtual constexpr function
      83. 4.9.4The new Character Type of UTF-8 Strings: char8_t
      84. char versus char8_t
      85. 4.9.5using enum in Local Scopes
      86. 4.9.6Default Member Initializers for Bit Fields
      87. Distilled Information

      5.The Standard Library

      1. 5.1The Ranges Library
      2. 5.1.1Ranges
      3. 5.1.2Views
      4. 5.1.3Range Adaptors
      5. Views on Temporary Ranges
      6. 5.1.4Direct on the Container
      7. 5.1.5Function Composition
      8. 5.1.6Lazy Evaluation
      9. Pull Pipelines
      10. 5.1.7Define a View
      11. Class Template Argument Deduction Guide
      12. 5.1.8std Algorithms versus std::ranges Algorithms
      13. 5.1.9Design Choices
      14. Distilled Information
      15. 5.2std::span
      16. 5.2.1Static versus Dynamic Extent
      17. Distinguish between std::span, std::ranges::range, std::ranges::view, and std::string_view
      18. 5.2.2Creation
      19. 5.2.3Automatically Deduces the Size of a Contiguous Sequence of Objects
      20. 5.2.4Modifying the Referenced Objects
      21. 5.2.5std::span’s Operations
      22. 5.2.6A Constant Range of Modifiable Elements
      23. 5.2.7Dangers of std::span
      24. Distilled Information
      25. 5.3Container and Algorithm Improvements
      26. 5.3.1constexpr Containers and Algorithms
      27. 5.3.2std::array
      28. 5.3.3Consistent Container Erasure
      29. 5.3.4contains for Associative Containers
      30. 5.3.5Shift the Content of a Container
      31. 5.3.6String prefix and suffix checking
      32. 5.3.7Vectorized Execution Policy: std::execution::unseq
      33. Distilled Information
      34. 5.4Arithmetic Utilities
      35. 5.4.1Safe Comparison of Integers
      36. Integral versus Integer
      37. 5.4.2Mathematical Constants
      38. 5.4.3Midpoint and Linear Interpolation
      39. 5.4.4Bit Manipulation
      40. Distilled Information
      41. 5.5Formatting Library
      42. 5.5.1Formatting Functions
      43. 5.5.2Format String
      44. 5.5.3User-Defined Types
      45. 5.5.4Internationalization
      46. Distilled Information
      47. 5.6Calendar and Time Zones
      48. 5.6.1Basic Chrono Terminology
      49. 5.6.2Basic Types and Literals
      50. 5.6.3Time of Day
      51. 5.6.4Calendar Dates
      52. 5.6.5Time Zones
      53. Challenges
      54. 5.6.6Chrono I/O
      55. Format String must be a C++ String
      56. Distilled Information
      57. 5.7Further Improvements
      58. 5.7.1std::bind_front
      59. std::bind_front versus std::bind
      60. 5.7.2std::is_constant_evaluated
      61. 5.7.3std::ssize
      62. 5.7.4std::source_location
      63. 5.7.5std::to_address
      64. Distilled Information

      6.Concurrency

      1. 6.1Coroutines
      2. The Challenge of Understanding Coroutines
      3. 6.1.1A Generator Function
      4. 6.1.2Characteristics
      5. Distinguish Between the Coroutine and the Coroutine Handle
      6. 6.1.3The Framework
      7. 6.1.4Awaitables and Awaiters
      8. awaiter = awaitable
      9. 6.1.5The Workflows
      10. 6.1.6co_return
      11. 6.1.7co_yield
      12. 6.1.8co_await
      13. Distilled Information
      14. 6.2Atomics
      15. 6.2.1std::atomic_ref
      16. 6.2.2Atomic Smart Pointer
      17. The Importance of being Thread Safe
      18. 6.2.3std::atomic_flag Extensions
      19. 6.2.4std::atomic Extensions
      20. Condition Variables versus Promise/Future Pairs versus std::atomic_flag
      21. Distilled Information
      22. 6.3Semaphores
      23. Edsger W. Dijkstra invented Semaphores
      24. Distilled Information
      25. 6.4Latches and Barriers
      26. 6.4.1std::latch
      27. 6.4.2std::barrier
      28. Distilled Information
      29. 6.5Cooperative Interruption
      30. Killing a Thread is Dangerous
      31. 6.5.1std::stop_source
      32. 6.5.2std::stop_token
      33. 6.5.3std::stop_callback
      34. 6.5.4A General Mechanism to Send Signals
      35. 6.5.5Joining Threads
      36. 6.5.6New wait Overloads for the condition_variable_any
      37. Distilled Information
      38. 6.6std::jthread
      39. 6.6.1Automatically Joining
      40. 6.6.2Cooperative Interruption of a std::jthread
      41. Distilled Information
      42. 6.7Synchronized Output Streams
      43. Compiler Support for Synchronized Output Streams
      44. std::cout is thread-safe
      45. Distilled Information

      7.Case Studies

      1. 7.1A Flavor of Python
      2. 7.1.1filter
      3. 7.1.2map
      4. 7.1.3List Comprehension
      5. 7.2Variations of Futures
      6. 7.2.1A Lazy Future
      7. Lifetime Challenges of Coroutines
      8. 7.2.2Execution on Another Thread
      9. 7.3Modification and Generalization of a Generator
      10. 7.3.1Modifications
      11. 7.3.2Generalization
      12. 7.3.3Iterator Protocol
      13. 7.4Various Job Workflows
      14. 7.4.1The Transparent Awaiter Workflow
      15. 7.4.2Automatically Resuming the Awaiter
      16. 7.4.3Automatically Resuming the Awaiter on a Separate Thread
      17. 7.5Fast Synchronization of Threads
      18. The Reference PCs
      19. About the Numbers
      20. 7.5.1Condition Variables
      21. 7.5.2std::atomic_flag
      22. 7.5.3std::atomic<bool>
      23. 7.5.4Semaphores
      24. 7.5.5All Numbers
      25. Distilled Information
      26. IVEpilogue
      27. VFurther Information

      8.C++23 and Beyond

      1. 8.1C++23
      2. 8.1.1Core Language
      3. 8.1.2The Standard Library
      4. Comparison of the Flat Ordered Associative Container and their Non-Flat Pendants
      5. 8.2Beyond C++23
      6. 8.2.1Contracts
      7. Closing Thoughts from Herb Sutter
      8. 8.2.2Reflection
      9. 8.2.3Pattern Matching

      9.Feature Testing

      1. Feature Support

      10.Glossary

      1. 10.1Aggregate
      2. 10.2Automatic Storage Duration
      3. 10.3Awaitable
      4. 10.4Awaiter
      5. 10.5Callable
      6. 10.6Callable Unit
      7. 10.7Concurrency
      8. 10.8Critical Section
      9. 10.9Data Race
      10. 10.10Deadlock
      11. 10.11Dynamic Storage Duration
      12. 10.12Eager Evaluation
      13. 10.13Executor
      14. 10.14Function Objects
      15. Instantiate function objects to use them
      16. 10.15Lambda Expressions
      17. 10.16Lazy Evaluation
      18. 10.17Literal Type
      19. 10.18Lock-free
      20. 10.19Lost Wakeup
      21. 10.20Math Laws
      22. 10.21Memory Location
      23. 10.22Memory Model
      24. 10.23Non-blocking
      25. 10.24Object
      26. 10.25Parallelism
      27. 10.26POD (Plain Old Data)
      28. 10.27Predicate
      29. 10.28RAII
      30. 10.29Race Conditions
      31. 10.30Regular Type
      32. 10.31Scalar Type
      33. 10.32SemiRegular
      34. 10.33Short-Circuit Evaluation
      35. 10.34Standard-Layout Type
      36. 10.35Static Storage Duration
      37. 10.36Spurious Wakeup
      38. 10.37The Big Four
      39. 10.38The Big Six
      40. 10.39Thread
      41. 10.40Thread Storage Duration
      42. 10.41Time Complexity
      43. 10.42Translation Unit
      44. 10.43Trivial Type
      45. 10.44Type Erasure
      46. 10.45Undefined Behavior

      Index

      1. A
      2. B
      3. C
      4. DE
      5. FG
      6. HIJKL
      7. M
      8. NOPR
      9. S
      10. T
      11. UV
      12. WYZ

      Get the free sample chapters

      Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

      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