What’s new in C++ 20: modules, concepts, and coroutines

Many C++ 20 features already are in experimental or production use

What’s new in C++ 20: modules, concepts, and coroutines
Pixabay (CC0)

C++ 20, the latest version of the venerable general-purpose systems programming language, has been deemed feature-complete by the International Organization for Standardization (ISO), with capabilities including modules, concepts, and coroutines.

Both concepts and modules will simplify the use of C++ once developers learn them and get over “teething pains,” said C++ founder Bjarne Stroustrup.

Modules let programmers specify and use modular components, so C++ developers can finally move away from the complex, error-prone use of preprocessing for program composition.

Concepts let programmers specify a template’s requirements on its arguments. This fulfills the intention for templates to support generic programming by making their interfaces precise. Code quality will be improved and templates made easier to use, Stroustrup said.

Coroutines are program components that generalize subroutines for use in nonpreemptive multitasking, letting execution be suspended and resumed. Suited for tasks such as event loops and iterators, coroutines are enabled by extensions that include new syntactic forms and modifications to existing semantics in the language.

Other new features in C++ 20 include:

  • A C++ 20 synchronization library, bringing barriers, latches, and a few other modifications to C++’s low-level concurrent programming support. Intended for expert programmers, this library is part of the general push for better support of fine-grained control on modern hardware.
  • Various compile-time computation improvements.
  • Ranges, providing components for dealing with ranges of elements, including a variety of view adapters.
  • Contracts, specifying pre and post conditions and assertions for functions.
  • Feature test macros, defining a set of macros corresponding to language and library features introduced in C++ 11 or later.
  • Precalculated hash values in lookup, extending the interface of unordered containers with member functions that have one additional argument taking a precalculated hash value for the value being queried. The rationale for this proposal is the notion that in business scenarios the same keyword often has to be searched on in more than one container at a time. This has been an expensive proposition so far, forcing hash-value recalculation on each lookup.
  • Expansion statements, enabling the compile-time repetition of a statement for each element of a tuple, array, class, parameter pack, or range.
  • Monadic operations for std::optional. Some uses of std::optional, an important vocabulary type, are verbose and would benefit from operations allowing functional composition.
  • Implicit creation of objects for low-level object manipulation, in which objects of sufficiently trivial types be created on-demand as necessary within newly allocated storage to give programs defined behavior.

The ISO’s formal adoption of C++ 20 is expected by 2020. But many C++ 20 features already are in experimental or production use, such as concepts in the GCC (GNU Compiler Collection) and coroutines in Clang C++, Stroustrup said. The language itself will turn 40 years old this year, having debuted as C with Classes in fall 1979. The name was changed to C++ in 1984.

Copyright © 2019 IDG Communications, Inc.

InfoWorld Technology of the Year Awards 2023. Now open for entries!