my-server
← Wiki

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 (based on the C99 feature, and common g++ extension)
  • <code>[=, this]</code> as a lambda capture
  • template parameter lists on lambdas
  • three-way comparison using the "spaceship operator", <code>operator<=></code>
  • initialization of an additional variable within a range-based <code>for</code> statement
  • 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 <code>typename</code> in certain circumstances
  • new standard attributes <code><nowiki>no_unique_address</nowiki></code>, <code><nowiki>likely</nowiki></code> and <code><nowiki>unlikely</nowiki></code>
  • conditional <code>explicit</code>, allowing the <code>explicit</code> modifier to be contingent on a Boolean expression
  • expanded <code>constexpr</code>: virtual functions, <code>union</code>, <code>try</code> and <code>catch</code>, <code>dynamic_cast</code> and <code>typeid</code>, <code>std::pointer_traits</code>
  • immediate functions using the new <code>consteval</code> keyword
  • signed integers are now defined to be represented using two's complement (signed integer overflow remains undefined behavior)
  • a revised memory model
  • various improvements to structured bindings (interaction with lambda captures, <code>static</code> and <code>thread_local</code> storage duration)
  • coroutines
  • <code>using</code> on scoped <code>enum</code>s
  • <code>constinit</code> keyword

Library

  • ranges (The One Ranges Proposal)
  • <code>std::make_shared</code> and <code>std::allocate_shared</code> for arrays
  • atomic smart pointers (such as <code>std::atomic<shared_ptr<T>></code> and <code>std::atomic<weak_ptr<T>></code>)
  • <code>std::to_address</code> to convert a pointer to a raw pointer
  • calendar and time-zone additions to <code><chrono></code>
  • <code>std::span</code>, providing a view to a contiguous array (analogous to <code>std::string_view</code> but <code>span</code> can mutate the referenced sequence)
  • <code>std::erase</code> and <code>std::erase_if</code>, simplifying element erasure for most standard containers
  • <code><version></code> header
  • <code>std::bit_cast<></code> for type casting of object representations, with less verbosity than <code>memcpy()</code> and more ability to exploit compiler internals
  • feature test macros
  • various constexpr library bits
  • smart pointer creation with default initialization
  • <code>contains()</code>-method for associative containers
  • prefix and suffix checking for strings
  • bit operations, such as leading/trailing zero/one count, and log2 operations
  • <code>std::bind_front</code>
  • <code>std::atomic::wait</code>, <code>std::atomic::notify_one</code>, and<code>std::atomic::notify_all</code> were added, giving the standard library futex-like capabilities

New and changed keywords

Eight new keywords added (plus the new "spaceship operator", <code>operator<=></code>), such as <code>concept</code>, <code>constinit</code>, <code>consteval</code>, <code>requires</code> (plus changed meaning for <code>export</code>), and <code>char8_t</code> (for UTF-8 support); of the eight new these three <code>co_await</code>, <code>co_return</code>, <code>co_yield</code>, are only for the new coroutine support (additionally six previously existing keywords have changed or new meaning added); <code>explicit</code> can take an expression since C++20. Most of the uses of the <code>volatile</code> keyword have been deprecated.

In addition to keywords, there are identifiers with special meaning (effectively keywords in certain contexts), including new <code>import</code> and <code>module</code>.

New attributes in C++20: <code><nowiki>likely</nowiki></code>, <code><nowiki>unlikely</nowiki></code>, and <code><nowiki>no_unique_address</nowiki></code>

Removed and deprecated

Removed features:

  • The C-derived headers <code><ccomplex></code>, <code><ciso646></code>, <code><cstdalign></code>, <code><cstdbool></code> and <code><ctgmath></code> were removed, as they serve no purpose in C++. (The corresponding <code><*.h></code> headers remain, for compatibility with C.)
  • The use of <code>throw</code> clauses was removed.
  • Some previously deprecated library features were removed, including <code>std::uncaught_exception</code>, <code> std::raw_storage_iterator</code>, <code>std::is_literal_type</code>, <code>std::is_literal_type_v</code>, <code>std::result_of</code> and <code>std::result_of_t</code>.

Deprecated features:

  • Use of comma operator in subscript expressions has been deprecated
  • (most of) <code>volatile</code> has been deprecated

Published as Technical Specifications

  • Parallelism TS v2 (including task blocks)
  • Reflection TS v1
  • Networking TS v1

Deferred to a later standard

  • Contracts a new study group (SG21) 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 (blocked by executors)
  • Properties
  • Extended futures

Compiler support

Full support

  • Visual Studio 2019 supports all C++20 features through its <code>/std:c++latest</code> option, as of version 16.10.0. An option <code>/std:c++20</code> 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.

Partial

  • Clang has partial C++20 support that can be enabled with the option <code>-std=c++20</code> (version 10 and later) or <code>-std=c++2a</code> (version 9 and earlier).
  • 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 <code>-std=c++2a</code>. Like Clang, GCC replaced this option with <code>-std=c++20</code> in version 10. It also has an option to enable GNU extensions in addition to the experimental C++20 support, <code>-std=gnu++20</code>.

History

Changes applied to the C++20 working draft in July 2017 (Toronto) include:

  • concepts (what made it into the standard is a cut-down version; also described as "Concepts Lite")
  • designated initializers
  • <code>[=, this]</code> as a lambda capture
  • template parameter lists on lambdas
  • <code>std::make_shared</code> and <code>std::allocate_shared</code> for arrays

Changes applied to the C++20 working draft in the fall meeting in November 2017 (Albuquerque) include:

  • three-way comparison using the "spaceship operator", <code>operator &lt;=&gt;</code>
  • initialization of an additional variable within a range-based <code>for</code> 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 (such as <code>std::atomic<shared_ptr<T>></code> and <code>std::atomic<weak_ptr<T>></code>)
  • <code>std::to_address</code> to convert a pointer to a raw pointer

Changes applied to the C++20 working draft in March 2018 (Jacksonville) include:

  • removing the need for <code>typename</code> in certain circumstances
  • new standard attributes <code><nowiki>no_unique_address</nowiki></code>, <code><nowiki>likely</nowiki></code> and <code><nowiki>unlikely</nowiki></code>
  • calendar and time-zone additions to <code><chrono></code>
  • <code>std::span</code>, providing a view to a contiguous array (analogous to <code>std::string_view</code> but <code>span</code> can mutate the referenced sequence)
  • <code><version></code> header

Changes applied to the C++20 working draft in the summer meeting in June 2018 (Rapperswil) include:

  • <s>contracts</s> (later deferred to a later standard, laterly came back in C++26)
  • feature test macros
  • bit-casting of object representations, with less verbosity than <code>memcpy()</code> and more ability to exploit compiler internals
  • conditional <code>explicit</code>, allowing the explicit modifier to be contingent on a Boolean expression
  • constexpr virtual functions

Changes applied to the C++20 working draft in the fall meeting in November 2018 (San Diego) include:

  • ranges (The One Ranges Proposal)
  • concept terse syntax
  • <code>constexpr</code> <code>union</code>, <code>try</code> and <code>catch</code>, <code>dynamic_cast</code>, <code>typeid</code> and <code>std::pointer_traits</code>.
  • various <code>constexpr</code> library bits
  • immediate functions using the new <code>consteval</code> keyword
  • signed integers are now defined to be represented using two's complement (signed integer overflow remains undefined behavior)
  • <s>refinements of the contracts facility (access control in contract conditions)</s> (see list of features deferred to a later standard)
  • a revised memory model
  • smart pointer creation with default initialization

Changes applied to the C++20 working draft in the winter meeting in February 2019 (Kona) include:

  • coroutines
  • modules
  • various improvements to structured bindings (interaction with lambda captures, <code>static</code> and <code>thread_local</code> storage duration)

Changes applied to the C++20 working draft in the summer meeting in July 2019 (Cologne) include:

  • contracts were removed (see list of features deferred to a later standard)
  • use of comma operator in subscript expressions has been deprecated
  • <code>constexpr</code> additions (trivial default initialization, unevaluated inline-assembly)
  • using scoped <code>enum</code>s
  • various changes to the spaceship operator
  • DR: minor changes to modules
  • <code>constinit</code> keyword
  • changes to concepts (removal of <code>-> Type</code> return-type-requirements)
  • (most of) <code>volatile</code> has been deprecated
  • DR: <code><nowiki>nodiscard</nowiki></code> effects on constructors
  • The new standard library concepts will not use PascalCase (rather <code>standard_case</code>, as the rest of the standard library)
  • text formatting (<code>std::format</code>, <code>chrono</code> integration, corner case fixes)
  • bit operations
  • <code>constexpr INVOKE</code>
  • math constants
  • consistency additions to atomics (<code>std::atomic_ref<T></code>, <code>std::atomic<std::shared_ptr<T>></code>)
  • add the <code><=></code> operator to the standard library
  • header units for the standard library
  • synchronization facilities (merged from: Efficient atomic waiting and semaphores, latches and barriers, Improving <code>atomic_flag</code>, Don't Make C++ Unimplementable On Small CPUs)
  • <code>std::source_location</code>
  • <code>constexpr</code> containers (<code>std::string</code>, <code>std::vector</code>)
  • <code>std::stop_token</code> and joining thread (<code>std::jthread</code>)

Changes applied during the NB comment resolution in the fall meeting in November 2019 (Belfast) include:

  • Class Types in Non-Type Template Parameters (NTTP): The restriction of no user-defined <code>operator==</code> allowed has been removed as the meaning of template argument equality has been divorced from <code>operator==</code>. This allows also for array members in class-type NTTP.
  • Floating-point types, pointers and references and unions and union-like classes (class types containing anonymous unions) are now allowed as NTTP.
  • Function identity now also includes trailing requires-clauses (P1971)
  • Constrained non-template functions have been removed
  • <code>&lt;compare&gt;</code> is now available in freestanding implementations
  • <code>std::span</code>s typedef was changed from <code>index_type</code> to <code>size_type</code> 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 (P1456R1: Move-only views, P1391R4: Range constructor for <code>std::string_view</code> (constructor from iterator-pair of characters), P1394R4: Range constructor for <code>std::span&lt;ref&gt;</code>, P1870R1: forwarding-range&lt;T&gt; is too subtle)
  • Initialization for <code>std::atomic<T></code> has been changed to make it work with default and list initialization, <code>std::latch</code> and <code>std::barrier</code> can now report the maximum number of threads that the implementation supports through the new member function <code>max()</code>
  • <code>std::weak_equality</code> and <code>std::strong_equality</code> have been removed as they are not used anymore
  • Algorithms in <code>&lt;numeric&gt;</code> have been made <code>constexpr</code>
  • Missing feature-test macros for new or changed features of C++20 have been added

References

External links