C++20
C++20 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++20 replaced the prior version of the C++ standard, called C++17, and was later replaced by C++23. The standard was technically finalized by WG21 at the meeting in Prague in February 2020, had its final draft version announced in March 2020, was approved on 4 September 2020, and published in December 2020.
Features
C++20 adds more new major features than C++14 or C++17. Changes that have been accepted into C++20 include: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
forstatement - lambdas in unevaluated contexts
- default constructible and assignable stateless lambdas
- allow pack expansions in lambda init-capture
- class types in non-type template parameters, also allowing string literals as template parameters
- removing the need for
typenamein certain circumstances - new standard attributes
,no_unique_address andlikely unlikely - conditional
explicit, allowing theexplicitmodifier to be contingent on a Boolean expression - expanded
constexpr: virtual functions,union,tryandcatch,dynamic_castandtypeid,std::pointer_traits - immediate functions using the new
constevalkeyword - signed integers are now defined to be represented using two's complement
- a revised memory model
- various improvements to structured bindings
- coroutines
usingon scopedenumsconstinitkeyword
Library
- ranges
std::make_sharedandstd::allocate_sharedfor arrays - atomic smart pointers
std::to_addressto convert a pointer to a raw pointer - calendar and time-zone additions to
,std::span, providing a view to a contiguous arraystd::eraseandstd::erase_if, simplifying element erasure for most standard containersheaderstd::bit_cast<>for type casting of object representations, with less verbosity thanmemcpyand more ability to exploit compiler internals - feature test macros
- various constexpr library bits
- smart pointer creation with default initialization
contains-method for associative containers - prefix and suffix checking for strings
- bit operations, such as leading/trailing zero/one count, and log2 operations
std::bind_front,std::atomic::wait,std::atomic::notify_one, andstd::atomic::notify_allwere added, giving the standard library futex-like capabilities
New and changed keywords
Eight new keywords added, such asconcept, constinit, consteval, requires, and char8_t ; of the eight new these three co_await, co_return, co_yield, are only for the new coroutine support ; 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 Removed and deprecated
Removed features:- The C-derived headers
,,,andwere removed, as they serve no purpose in C++. - The use of
throwclauses was removed. - Some previously deprecated library features were removed, including
std::uncaught_exception,std::raw_storage_iterator,std::is_literal_type,std::is_literal_type_v,std::result_ofandstd::result_of_t.
- Use of comma operator in subscript expressions has been deprecated
volatilehas been deprecated
Published as Technical Specifications
- Parallelism TS v2
- Reflection TS v1
- Networking TS v1
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
Compiler support
Full support- Visual Studio 2019 supports all C++20 features through its
/std:c++latestoption, as of version 16.10.0. An option/std:c++20to enable C++20 mode is added in version 16.11.0.
Partial
- Clang has partial C++20 support that can be enabled with the option
-std=c++20or-std=c++2a. - EDG started implementing C++20 features in version 5.0 and as of version 6.1 supports most C++20 core language features.
- GCC added partial, experimental C++20 support in 2017 in version 8 through the option
-std=c++2a. Like Clang, GCC replaced this option with-std=c++20in version 10. It also has an option to enable GNU extensions in addition to the experimental C++20 support,-std=gnu++20.
History
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_sharedandstd::allocate_sharedfor arrays
- three-way comparison using the "spaceship operator",
operator <=> - initialization of an additional variable within a range-based
forstatement - 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_addressto convert a pointer to a raw pointer
- removing the need for
typenamein certain circumstances - new standard attributes
,no_unique_address andlikely unlikely - calendar and time-zone additions to
,std::span, providing a view to a contiguous arrayheader
- feature test macros
- bit-casting of object representations, with less verbosity than
memcpyand 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
constexprunion,tryandcatch,dynamic_cast,typeidandstd::pointer_traits. - various
constexprlibrary bits - immediate functions using the new
constevalkeyword - 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
Changes applied to the C++20 working draft in the summer meeting in July 2019 include:
- contracts were removed
- use of comma operator in subscript expressions has been deprecated
constexpradditions - using scoped
enums - various changes to the spaceship operator
- DR: minor changes to modules
constinitkeyword - changes to concepts
volatilehas 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,constexprcontainersstd::stop_tokenand joining thread
- Class Types in Non-Type Template Parameters : The restriction of no user-defined
operatorallowed 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 implementationsstd::spans typedef was changed fromindex_typetosize_typeto 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::atomichas been changed to make it work with default and list initialization,std::latchandstd::barriercan now report the maximum number of threads that the implementation supports through the new member functionmax,std::weak_equalityandstd::strong_equalityhave 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