C++20
C++20 is the informal name for the revision of the ISO/IEC standard for the C++ programming language expected to follow C++17. The standard became technically finalized by WG21 at the meeting in Prague in February 2020. The standard was expected to be officially published after the end of the DIS ballot in May 2020, but as of July 2020, it is still underway.
C++20 adds more new major features than C++14 or C++17. Below is a partial list of changes that have been accepted into or have been discussed for inclusion into C++20.
New Features
C++20 introduced many new features. The following lists may be incomplete.Language
- concepts, with terse syntax
- modules
- designated initializers
-
as a lambda capture
- template parameter lists on lambdas
- three-way comparison using the "spaceship operator",
operator <=>
- initialization of an additional variable within a range-based
for
statement - lambdas in unevaluated contexts
- default constructible and assignable stateless lambdas
- allow pack expansions in lambda init-capture
- string literals as template parameters
- removing the need for
typename
in certain circumstances - new standard attributes
,no_unique_address
andlikely unlikely - conditional
explicit
, allowing the explicit modifier to be contingent on a boolean expression - expanded
constexpr
: virtual functions, union, try and catch, dynamic_cast and typeid, std::pointer_traits - immediate functions using the new
consteval
keyword - signed integers are now defined to be represented using two's complement
- a revised memory model
- various improvements to structured bindings
- coroutines
-
using
on scoped enums -
constinit
keywordLibrary
- ranges
-
std::make_shared
andstd::allocate_shared
for arrays - atomic smart pointers
-
std::to_address
to convert a pointer to a raw pointer - calendar and time-zone additions to
-
std::span
, providing a view to a contiguous array -
header -
std::bit_cast<>
for type casting of object representations, with less verbosity thanmemcpy
and more ability to exploit compiler internals - feature test macros
- various constexpr library bits
- smart pointer creation with default initialization
-
std::map::contains
methodDeprecation
- Use of comma operator in subscript expressions has been deprecated
-
volatile
has been deprecatedNew (and changed) keywords
concept
, constinit
, consteval
, co_await
, co_return
, co_yield
, requires
, and char8_t
. And explicit
can take an expression since C++20. Most of the uses of the volatile
keyword have been deprecated.In addition to keywords, there are identifiers with special meaning, including new
import
and module
.New attributes in C++20:
likely
,unlikely
,and
no_unique_address
History of changes
Changes applied to the C++20 working draft in July 2017 include:- concepts
- designated initializers
-
as a lambda capture
- template parameter lists on lambdas
-
std::make_shared
andstd::allocate_shared
for arrays
- three-way comparison using the "spaceship operator",
operator <=>
- initialization of an additional variable within a range-based
for
statement - lambdas in unevaluated contexts
- default constructible and assignable stateless lambdas
- allow pack expansions in lambda init-capture
- string literals as template parameters
- atomic smart pointers
-
std::to_address
to convert a pointer to a raw pointer
- removing the need for
typename
in certain circumstances - new standard attributes
,no_unique_address
andlikely unlikely - calendar and time-zone additions to
-
std::span
, providing a view to a contiguous array -
header
- contracts [|deferred to a later standard]
- feature test macros
- bit-casting of object representations, with less verbosity than
memcpy
and more ability to exploit compiler internals - conditional
explicit
, allowing the explicit modifier to be contingent on a boolean expression - constexpr virtual functions
- ranges
- concept terse syntax
- constexpr union, try and catch, dynamic_cast, typeid and std::pointer_traits.
- various constexpr library bits
- immediate functions using the new
consteval
keyword - signed integers are now defined to be represented using two's complement
refinements of the contracts facility- a revised memory model
- Smart pointer creation with default initialization
- coroutines already experimentally supported in Clang 5
- modules experimentally supported in Clang 5 and Visual Studio 2015 Update 1 as well as GCC
- various improvements to structured bindings
- contracts have been removed
- use of comma operator in subscript expressions has been deprecated
- constexpr additions
- using scoped enums
- various changes to the spaceship operator
- DR: minor changes to modules
-
constinit
keyword - changes to concepts
-
volatile
has been deprecated - DR:
effects on constructorsnodiscard - The new standard library concepts will not use PascalCase
- text formatting
- bit operations
-
constexpr INVOKE
- math constants
- consistency additions to atomics
- add the
<=>
operator to the standard library - header units for the standard library
- synchronization facilities
-
std::source_location
- constexpr containers
-
std::stop_token
and joining thread
- Class Types in Non-Type Template Parameters : The restriction of no user-defined
operator
allowed has been removed as the meaning of template argument equality has been divorced fromoperator
. This allows also for array members in class-type NTTP. - Floating-point types, pointers and references and unions and union-like classes are now allowed as NTTP.
- Function identity now also includes trailing requires-clauses
- Constrained non-template functions have been removed
-
<compare>
is now available in freestanding implementations std::span
s typedef was changed fromindex_type
tosize_type
to be consistent with the rest of the standard library- Concept traits have been renamed to follow the renaming of the concepts as a result from the Cologne meeting
- Several fixes and additions to ranges
- Initialization for
std::atomic<T>
has been changed to make it work with default and list initialization,std::latch
andstd::barrier
can now report the maximum number of threads that the implementation supports through the new member functionmax
-
std::weak_equality
andstd::strong_equality
have been removed as they are not used anymore - Algorithms in
<numeric>
have been madeconstexpr
- Missing feature-test macros for new or changed features of C++20 have been added
Features published as Technical Specifications
- Parallelism TS v2
- Networking TS v1
- Reflection TS v1
Features deferred to a later standard
- Contracts a new study group has been formed to work on a new proposal
- Reflection
- Metaclasses
- Executors
- Networking extensions, including async, basic I/O services, timers, buffers and buffer-oriented streams, sockets, and Internet protocols
- Properties
- Extended futures