What is new in C++20?
C++20 is the latest version of the C++ programming language, which was released in 2020. This significant update brings numerous new features, improvements, and standardization to the language. In this article, we’ll explore the new aspects of C++20, covering its key changes and updates.
New C++20 Features
• Modules: C++20 introduces a new standard way of organizing code: modules. These are a way to package related declarations (functions, classes, variables) into a single unit, similar to headers, but more powerful and efficient.
• Concepts: Concepts are an alternative to templates for solving similar problems. They add an additional layer of error detection and prevention.
• Coroutines: The introduction of coroutines enables developers to handle asynchronous code more efficiently using standard library support.
• Atomic Smart Pointers: Atomic smart pointers are a new mechanism for managing shared memory atomic operations.
• consteval: consteval is a new standard library function that can detect if a constant expression returns an empty string.
Key Changes in C++20
• Namespace namespace: The namespace specifier namespace has gained an additional keyword, called namespace namespace, indicating a new namespace.
• Literal suffixes: Type-specific literal suffixes allow users to specify the corresponding signed type.
• enum class: Enum class is a new and simplified way to create non-unscoped enumerations using enum class.
Highlights of C++20 Improvements
Lambda Capture
• Wildcard lambda capture: Wildcard lambda capture (=) allows you to specify that all variables without initializers should be inherited into the lambda.
Constant Evaluation
• Constant evaluation: constant evaluation allows the implementation to evaluate constant expressions statically.
• constexpr all over: constexpr, constinit, consteval, and _Atomic are used much more extensively throughout the new C++20 standard libraries.
C++ Coroutines
• Prologue and Epilogue Functionality: Prologue and Epilogue functionality: Allows for the implementation to have control over the functions used to start and clean up the coroutine.
• Context Switching Functions: Context switching functions enable the implementation of low-level context switching facilities.
• State Access and Manipulation: current_task() and other utilities for accessing and manipulation the state of the current fiber (coroutine).
• Error Handling: Catch exceptions in the prologue to prevent them from exiting the coroutine.
Atomic Containers
• std::atomic: std::atomic is now applicable to any type, in addition to the basic data types.
• Atomics for containers: In-place modification of container (vectors, lists etc) using atomic operations like atomic::exchange.
| Table 1: New Features Added to C++20: | Feature | Description |
|---|---|---|
| Modules | Package and import code in a controlled way | |
| Concepts | Introduce a new generic programming model | |
| Coroutines | Support for coroutine programming | |
| Atomic Containers | Add atomic operations support to containers | |
| new constexpr | Support for compilation time evaluation |
Where to Go from Here
As C++20 sets a new standard for the world of C++ programming, there are many ways developers can leverage these new features. For those who seek more information, here is a list of resources
- ISO/IEC 14882: For those who want direct access to the official spec of C++20 can check the ISO/IEC 14882 spec, which is the standardized form of the C++ programming language.
- cppreference.com: An online reference manual provided by cppreference.com on all C++ standard including C++20.
Conclude by stating that a clear understanding of these recent additions to the C++20 standard will give readers the tools to better learn, implement, and debug modern C++20 systems