Talk review: C++20: An (Almost) Complete Overview

C++20: An (Almost) Complete Overview – Marc Gregoire – CppCon 2020

C++20: An (Almost) Complete Overview – Marc Gregoire – CppCon 2020

Review

In this talk from CppCon 2020 Marc Gregoire gives a brief overview of the changes coming to C++20. The new standard will be bigger than any other (except for the first one of course). The range of changes is enormous. It starts from big rocks like modules & concepts and ends with small enhancements like the unified function to remove items from containers. In many ways, it will revolutionize or at least significantly change the way we think and write programs in C++.

Marc did a great job in showing many of these additions in only an hour-long keynote. The presentation is dynamic but easy to follow and understand. A large number of shown features forced the speaker to only mention their existence, rather than give a thorough explanation. However, wherever possible, Marc added a representative sample code, to illustrate how will the “new normal” look once C++20 is commonly used.

As for the time of publishing this article, C++20 is formally not an International Standard (IS) and full compilers support is still incomplete. Main implementations miss many important features. Nevertheless, C++20 will be available for the average programmer in the near time and many of us will have some contact with it sooner than later. I recommend watching this talk to prevent being surprised and to get excited about the wonderful future.

In this talk you will learn

  • what are the most important changes in C++20.

Talk timeline

  • 1:34 – Agenda.
  • 2:46 – New keywords & identifiers.
  • 3:55 – Modules:
    • explicit export of parts of the module,
    • modules are processed only once, which will result in faster builds,
    • the order of modules is not important.
  • 6:45 – Example of modules code.
  • 7:38 – STL in C++20 will not support modules:
    • However, all STL headers will be importable (can be imported) as if they were modules that export everything.
  • 9:07 – Ranges:
    • Definition of the range – object referring to the sequence of elements.
  • 9:36 – Quick example of the use case for ranges.
  • 10:43 – Components of ranges framework:
    • range,
    • range-based algorithms,
    • projections,
    • views,
    • range factories,
    • pipelining.
  • 12:14 – Example of ranges pipelining.
  • 15:02 – Coroutines.
    • Definition of the coroutine – function that contains one of the 3 keywords: co_await, co_yield and co_return.
  • 15:51 – Use cases for coroutines:
    • generators,
    • asynchronous I/O,
    • lazy computations,
    • event-driven applications.
  • 16:12 – C++20 has support for coroutines, but there is no support for it in the Standard Library:
    • Committee wants to have more experience with the usage of coroutines before standardizing anything.
  • 17:10 – Example of a coroutine.
  • 19:00 – Concepts:
    • Named requirements to constrain the template parameters.
    • Predicates evaluated at compile time.
  • 19:23 – Example of concepts:
    • defining concepts,
    • using concepts,
    • combining concepts,
    • STL defines a lot ready to use concepts.
  • 22:42 – Concepts improve error messages produced by the compiler.
  • 24:00 – Changes to lambda capture:
    • need to explicitly capture this,
    • breaking change.
  • 24:22 – Templated lambda expressions.
  • 26:49 – Changes to constexpr:
    • constexpr virtual functions (!),
    • dynamic memory allocation,
    • std::string & std::vector are constexpr.
  • 27:54 – Changes to concurrency:
    • atomic smart pointers (std::atomic<std::shared_ptr<T>>).
  • 29:02 – Example of atomic smart pointers.
  • 30.02 – Support for cancellable threads (aka std::jthread):
    • how cancellation works,
    • utilities, that can be used with std::jthreads.
  • 31:26 – Examples of std::jthreads.
  • 33:33 – Semaphores.
  • 34:10 – Latches.
  • 34:57 – Barriers.
  • 35:26 – Changes to std::atomic and std::atomic_ref.
  • 36:05 – Designated initializers.
  • 36:36 – “Spaceship operator”:
    • how it works,
    • how to check the comparison result,
    • examples.
  • 39:58 – Range-based for loop initializers.
  • 40:44 – Non-type template parameters:
    • C-Strings as template parameters!!!
  • 41:44 – Hints for compiler optimizations.
  • 42:06 – Support for calendars & timezones:
    • Gregorian calendar only,
    • new std::chrono types and literals,
    • Monday[3] / September / 2020,
    • new clocks (utc_clock, tai_clock, gpc_clock, file_clock),
    • time zone conversions.
  • 45:28std::span – view over some contiguous data:
    • examples,
    • an overview of the API.
  • 47:31 – Feature test macros.
  • 48:36<version> header.
  • 49:15 – Immediate functions:
    • functions, that can be executed only at compile time,
    • consteval.
  • 50:26constinit.
  • 51:20using enum.
  • 52:08 – Text formatting (std::format).
  • 54:12 – Math constants.
  • 54:31std::source_location.
  • 55:31[[nodiscard(<reason>)]].
  • 55:55 – Bit operations (available until now as compiler builtins).
  • 56:41 – Small STL additions.

Subscribe to get notified about new content

Thanks for taking your time to read it. If you like it or have other opinions, please share it in the comments. You can also subscribe to my newsletter to get latest news about my content. Happy coding and let’s stay connected!