| C++ language revisions |
|---|
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 as ISO/IEC 14882:2020 in December 2020.
GCC supports and defaults to C++20. Few if any other support C++20 fully, at least Clang has robust support, and claims partial support.
C++20 adds more new major features than C++14 or C++17. Changes that have been accepted into C++20 include:
export keyword, and introduce import and module as identifiers with special meaning[=, this] as a lambda captureoperator<=>for statementtypename in certain circumstances[[no_unique_address]], [[likely]] and [[unlikely]]explicit, allowing the explicit modifier to be contingent on a Boolean expressionconstexpr: virtual functions, union, try and catch, dynamic_cast and typeid, std::pointer_traitsconsteval keywordstatic and thread_local storage duration)using on scoped enumsconstinit keywordstd::make_shared and std::allocate_shared for arraysstd::atomic<shared_ptr<T>> and std::atomic<weak_ptr<T>>)std::to_address to convert a pointer to a raw pointer<chrono>std::span, providing a view to a contiguous array (analogous to std::string_view but span can mutate the referenced sequence)std::erase and std::erase_if, simplifying element erasure for most standard containers<version> header, providing feature test macrosstd::bit_cast<> for type casting of object representations, with less verbosity than memcpy() and more ability to exploit compiler internalsconstexpr library bitsstd::construct_at(), for calling a constructor at a specified memory location. This allows constexpr context usage for std::vector and std::string. Although this does necessarily allow declaring constexpr instances of std::string and std::vector (as their constructors are not constexpr), it does allow for these types to appear in constant evaluation contexts.contains()-method for associative containersstd::bind_frontstd::atomic::wait, std::atomic::notify_one, andstd::atomic::notify_all were added, giving the standard library futex-like capabilitiesEight new keywords added (plus the new "spaceship operator", operator<=>), such as concept, constinit, consteval, requires (as well as reintroducing the export keyword), and char8_t (for UTF-8 support); of the eight new these three co_await, co_return, co_yield, are only for the new coroutine support (additionally six previously existing keywords have changed or new meaning added); 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 (effectively keywords in certain contexts), including new import and module.
New attributes in C++20:
[[likely]], [[unlikely]], and [[no_unique_address]]
Removed features:
<ccomplex>, <ciso646>, <cstdalign>, <cstdbool> and <ctgmath> were removed, as they serve no purpose in C++. (The corresponding <*.h> headers remain, for compatibility with C.)throw clauses was removed, and are completely superseded by noexcept clauses.std::uncaught_exception, std::raw_storage_iterator, std::is_literal_type, std::is_literal_type_v, std::result_of and std::result_of_t.Deprecated features:
volatile has been deprecatedThe following compilers provide full support:
/std:c++latest option, as of version 16.10.0. An option /std:c++20 to enable C++20 mode is added in version 16.11.0.Microsoft's compiler supports not only Windows but also Linux, Android, and iOS. However, for Linux development, it requires the "Visual C++ for Linux Development" extension.
-std=c++20 (version 10 and later) or -std=c++2a (version 9 and earlier).-std=c++2a. Like Clang, GCC replaced this option with -std=c++20 in version 10. It also has an option to enable GNU extensions in addition to C++20 support, -std=gnu++20.Changes applied to the C++20 working draft in July 2017 (Toronto) include:
[=, this] as a lambda capturestd::make_shared and std::allocate_shared for arraysChanges applied to the C++20 working draft in the fall meeting in November 2017 (Albuquerque) include:
operator<=>for statementstd::atomic<shared_ptr<T>> and std::atomic<weak_ptr<T>>)std::to_address to convert a pointer to a raw pointerChanges applied to the C++20 working draft in March 2018 (Jacksonville) include:
typename in certain circumstances[[no_unique_address]], [[likely]] and [[unlikely]]<chrono>std::span, providing a view to a contiguous array (analogous to std::string_view but span can mutate the referenced sequence)<version> headerChanges applied to the C++20 working draft in the summer meeting in June 2018 (Rapperswil) include:
memcpy() and more ability to exploit compiler internalsexplicit, allowing the explicit modifier to be contingent on a Boolean expressionconstexpr virtual functionsChanges applied to the C++20 working draft in the fall meeting in November 2018 (San Diego) include:
constexpr union, try and catch, dynamic_cast, typeid and std::pointer_traits.constexpr library bitsconsteval keywordChanges applied to the C++20 working draft in the winter meeting in February 2019 (Kona) include:
static and thread_local storage duration)Changes applied to the C++20 working draft in the summer meeting in July 2019 (Cologne) include:
constexpr additions (trivial default initialization, unevaluated inline-assembly)enumsconstinit keyword-> Type return-type-requirements)volatile has been deprecated[[nodiscard]] effects on constructorsstd::format based on those of the fmtlib ({fmt}) library std::chrono integration, corner case fixes)constexpr invoke utilitiesstd::atomic_ref<T>, std::atomic<std::shared_ptr<T>>)<=> operator to the standard libraryatomic_flag, Don't Make C++ Unimplementable On Small CPUs)std::source_locationconstexpr containers (std::string, std::vector)std::stop_token and joining thread (std::jthread). std::jthread is a version of std::thread that automatically manages its own lifetime and supports cooperative interruption. The 'j' in its name stands for "joining" as its destructor calls join() (whereas C++11's std::thread calls std::terminate() if the thread is still running when its destructor is called).Changes applied during the NB comment resolution in the fall meeting in November 2019 (Belfast) include:
operator== allowed has been removed as the meaning of template argument equality has been divorced from operator==. This allows also for array members in class-type NTTP.<compare> is now available in freestanding implementationsstd::spans typedef was changed from index_type to size_type to be consistent with the rest of the standard librarystd::string_view (constructor from iterator-pair of characters), P1394R4: Range constructor for std::span<Ref>, P1870R1: forwarding-range<T> is too subtle)std::atomic<T> has been changed to make it work with default and list initialization, std::latch and std::barrier can now report the maximum number of threads that the implementation supports through the new member function max()std::weak_equality and std::strong_equality have been removed as they are not used anymore<numeric> have been made constexpr