C++20
This article has multiple issues. Please help or discuss these issues on the talk page. (Learn how and when to remove these template messages)
|
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.[1] The standard was technically finalized[2] by WG21 at the meeting in Prague in February 2020,[3] approved on 4 September 2020,[4][5] and published in December 2020.[6]
Features[]
C++20 adds more new major features than C++14 or C++17.[7] Changes that have been accepted into C++20 include:[8]
Language[]
- concepts,[9] with terse syntax[10]
- modules[11]
- designated initializers[12] (based on the C99 feature, and common g++ extension)
[=, this]
as a lambda capture[13]- template parameter lists on lambdas[14]
- three-way comparison using the "spaceship operator",
operator <=>
- initialization of an additional variable within a range-based
for
statement[15] - lambdas in unevaluated contexts[16][17]
- default constructible and assignable stateless lambdas[16][18]
- allow pack expansions in lambda init-capture[16][19]
- string literals as template parameters[16][20]
- removing the need for
typename
in certain circumstances[21] - new standard attributes
[[no_unique_address]]
,[22][[likely]]
and[[unlikely]]
[23] - conditional
explicit
, allowing the explicit modifier to be contingent on a boolean expression[24] - expanded
constexpr
: virtual functions,[25] union,[26] try and catch,[27] dynamic_cast and typeid,[28] std::pointer_traits[29] - immediate functions using the new
consteval
keyword[30] - signed integers are now defined to be represented using two's complement (signed integer overflow remains undefined behavior)[31]
- a revised memory model[32]
- various improvements to structured bindings (interaction with lambda captures, static and thread_local storage duration)[33][34]
- coroutines[35]
using
on scoped enums[36]constinit
keyword[37]
Library[]
- ranges (The One Ranges Proposal)[38]
std::make_shared
andstd::allocate_shared
for arrays[39]- atomic smart pointers (such as
std::atomic<shared_ptr<T>>
andstd::atomic<weak_ptr<T>>
)[40] std::to_address
to convert a pointer to a raw pointer[41]- calendar and time-zone additions to
<chrono>
[42] std::span
, providing a view to a contiguous array (analogous tostd::string_view
butspan
can mutate the referenced sequence)[43]std::erase
andstd::erase_if
, simplifying element erasure for most standard containers[44]<version>
header[45]std::bit_cast<>
for type casting of object representations, with less verbosity thanmemcpy()
and more ability to exploit compiler internals[46]- feature test macros[47]
- various constexpr library bits[48]
- smart pointer creation with default initialization[49]
std::map::contains
method[50]- bit operations, such as leading/trailing zero/one count,[51] and log2 operations[52][53][54]
std::bind_front
[55]
New and changed keywords[]
Many new keywords added (and the new "spaceship operator", operator <=>
), such as concept
, constinit
,[37] consteval
, co_await
, co_return
, co_yield
, requires
(plus changed meaning for export
), and char8_t
(for UTF-8 support).[56] And explicit
can take an expression since C++20.[57] Most of the uses of the volatile
keyword have been deprecated.[58]
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]]
[59]
Removed and deprecated[]
Removed features:[60]
- The C-derived headers
<ccomplex>
,<ciso646>
,<cstdalign>
,<cstdbool>
and<ctgmath>
were removed, as they serve no purpose in C++. (The corresponding<*.h>
headers remain, for compatibility with C.) - The use of
throw()
as an exception specification 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_of
andstd::result_of_t
.
Deprecated features:
- Use of comma operator in subscript expressions has been deprecated[61]
- (most of)
volatile
has been deprecated[58]
Published as Technical Specifications[]
Deferred to a later standard[]
- Contracts – a new study group (SG21) has been formed to work on a new proposal[66]
- Reflection[67][68]
- Metaclasses[69]
- Executors[70]
- Networking extensions,[71][72] including async, basic I/O services, timers, buffers and buffer-oriented streams, sockets, and Internet protocols (blocked by executors)
- Properties[73]
- Extended futures[74]
Compiler support[]
- Clang has partial C++20 support that can be enabled with the option
-std=c++20
(version 10 and later) or-std=c++2a
(version 9 and earlier).[75] - EDG eccp started implementing C++20 features in version 5.0 and as of version 6.1 supports most C++20 core language features.[76]
- GCC added partial, experimental C++20 support in 2017[77] in version 8 through the option
-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 the experimental C++20 support,-std=gnu++20
.[78] - Visual Studio 2019 supports all C++20 features through its
/std:c++latest
option, as of version 16.10.0.[79] An option/std:c++20
to enable C++20 mode is added in version 16.11.0.[80] [81]
History[]
Changes applied to the C++20 working draft in July 2017 (Toronto) include:[82]
- concepts (what made it into the standard is a cut-down version; also described as "Concepts Lite"[83])
- designated initializers
[=, this]
as a lambda capture- template parameter lists on lambdas
std::make_shared
andstd::allocate_shared
for arrays
Changes applied to the C++20 working draft in the fall meeting in November 2017 (Albuquerque) include:[84][85]
- 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 (such as
std::atomic<shared_ptr<T>>
andstd::atomic<weak_ptr<T>>
) std::to_address
to convert a pointer to a raw pointer
Changes applied to the C++20 working draft in March 2018 (Jacksonville) include:[86]
- removing the need for
typename
in certain circumstances - new standard attributes
[[no_unique_address]]
,[[likely]]
and[[unlikely]]
- calendar and time-zone additions to
<chrono>
std::span
, providing a view to a contiguous array (analogous tostd::string_view
butspan
can mutate the referenced sequence)<version>
header
Changes applied to the C++20 working draft in the summer meeting in June 2018 (Rapperswil) include:[87]
contracts(later deferred to a later standard)[88]- 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
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
- 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 (signed integer overflow remains undefined behavior)
refinements of the contracts facility (access control in contract conditions)[89] (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:[90][91]
- coroutines
- modules
- various improvements to structured bindings (interaction with lambda captures, static and thread_local storage duration)
Changes applied to the C++20 working draft in the summer meeting in July 2019 (Cologne) include:[92][93][94]
- contracts were removed (see list of features deferred to a later standard)[95]
- use of comma operator in subscript expressions has been deprecated[61]
- constexpr additions (trivial default initialization,[96] unevaluated inline-assembly[97])
- using scoped enums[36]
- various changes to the spaceship operator[98][99]
- DR: minor changes to modules[100]
constinit
keyword- changes to concepts (removal of
-> Type
return-type-requirements[101]) - (most of)
volatile
has been deprecated[58] - DR:
[[nodiscard]]
effects on constructors[102] - The new standard library concepts will not use PascalCase (rather standard_case, as the rest of the standard library)[103]
- text formatting[104][105] (chrono integration,[106] corner case fixes[107])
- bit operations[51]
constexpr INVOKE
[108]- math constants[109]
- consistency additions to atomics (
std::atomic_ref<T>
,[110]std::atomic<std::shared_ptr<T>>
[111]) - add the
<=>
operator to the standard library[112] - header units for the standard library[113]
- synchronization facilities[114] (merged from: Efficient atomic waiting and semaphores,[115] latches and barriers,[116] Improving atomic_flag,[117] Don't Make C++ Unimplementable On Small CPUs[118])
std::source_location
[119]- constexpr containers (
std::string
,[120]std::vector
[121]) std::stop_token
and joining thread (std::jthread
)[122]
Changes applied during the NB comment resolution in the fall meeting in November 2019 (Belfast) include:[123][124][125]
- Class Types in Non-Type Template Parameters (NTTP): The restriction of no user-defined
operator==
allowed has been removed as the meaning of template argument equality has been divorced fromoperator==
.[126] This allows also for array members in class-type NTTP. - Floating-point types,[127] 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
<compare>
is now available in freestanding implementations[128]std::span
s typedef was changed fromindex_type
tosize_type
to be consistent with the rest of the standard library[129]- 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,[130] P1391R4: Range constructor for
std::string_view
(constructor from iterator-pair of characters),[131] P1394R4: Range constructor forstd::span<ref>
,[132] P1870R1: forwarding-range<T> is too subtle[133]) - Initialization for
std::atomic<T>
has been changed to make it work with default and list initialization,[134]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[135]
References[]
- ^ "The Standard". isocpp.org. Retrieved 7 September 2020.
- ^ Sutter, Herb (2019-10-01). "P1000R3: C++ IS schedule" (PDF). Retrieved 2020-02-13.
- ^ Dusíková, Hana (2019-11-06). "N4817: 2020 Prague Meeting Invitation and Information" (PDF). Retrieved 2020-02-13.
- ^ "Current Status". isocpp.org. Retrieved 7 September 2020.
- ^ "C++20 Approved -- Herb Sutter". isocpp.org. Retrieved 8 September 2020.
- ^ "ISO/IEC 14882:2020".
- ^ "Why does the C++ standard ship every three years?".
- ^ "P0592R0: To boldly suggest an overall plan for C++20".
- ^ "P0606R0: Concepts Are Ready" (PDF).
- ^ "P1141R1 - Yet another approach for constrained declarations".
- ^ "N4720: Working Draft, Extensions to C++ for Modules" (PDF).
- ^ Tim Shen; Richard Smith. "Designated Initialization Wording".
- ^ Thomas Köppe. "Allow lambda capture [=, this]".
- ^ "Familiar template syntax for generic lambdas".
- ^ "Range-based for statements with initializer".
- ^ a b c d "Trip Report: C++ Standards Meeting in Albuquerque, November 2017". There's Waldo!. 2017-11-20. Retrieved 2017-12-11.
- ^ "Wording for lambdas in unevaluated contexts" (PDF).
- ^ "Default constructible and assignable stateless lambdas" (PDF).
- ^ "Pack expansion in lambda init-capture". www.open-std.org. Retrieved 2017-12-11.
- ^ "String literals as non-type template parameters" (PDF).
- ^ Nina Ranns; Daveed Vandevoorde. "Down with typename!".
- ^ "Language support for empty objects".
- ^ "Proposed wording for likely and unlikely attributes (Revision 5)".
- ^ "explicit(bool)". www.open-std.org. Retrieved 2018-11-13.
- ^ "Allowing Virtual Function Calls in Constant Expressions". www.open-std.org. Retrieved 2019-03-11.
- ^ "P1330R0 - Changing the active member of a union inside constexpr".
- ^ "P1002R0 - Try-catch blocks in constexpr functions" (PDF).
- ^ "P1327R0 - Allowing dynamic_cast, polymorphic typeid in Constant Expressions".
- ^ "P1006R1 - Constexpr in std::pointer_traits" (PDF).
- ^ "P1073R2 - Immediate functions".
- ^ "P1236R0: Alternative Wording for P0907R4 Signed Integers are Two's Complement".
- ^ "P0668R4: Revising the C++ memory model".
- ^ "P1091R1: Extending structured bindings to be more like variable declarations". www.open-std.org. Retrieved 2019-02-24.
- ^ "P1091R2: Extending structured bindings to be more like variable declarations". www.open-std.org. Retrieved 2019-02-24.
- ^ "N4649: Working Draft, Technical Specification on C++ Extensions for Coroutines" (PDF).
- ^ a b "P1099R5: Using Enum". www.open-std.org. 2019-07-20. Retrieved 2019-07-20.
- ^ a b "P1143R2: Adding the constinit keyword". www.open-std.org. 2019-07-20. Retrieved 2019-07-20.
- ^ "P0896R3" (PDF).
- ^ "Extending make_shared to Support Arrays".
- ^ Meredith, Alisdair; Sutter, Herb. "Revising atomic_shared_ptr for C++20". JTC1/SC22/WG21 - The C++ Standards Committee - ISOCPP. ISO. Retrieved 27 December 2018.
- ^ "Utility to convert a pointer to a raw pointer".
- ^ Howard E. Hinnant; Tomasz Kamiński. "Extending <chrono> to Calendars and Time Zones".
- ^ Neil MacIntosh; Stephan T. Lavavej. "span: bounds-safe views for sequences of objects".
- ^ Alisdair Meredith; Stephan T. Lavavej (2018-10-04). "Adopt Consistent Container Erasure from Library Fundamentals 2 for C++20".
- ^ Alan Talbot. "<version>".
- ^ "Bit-casting object representations". www.open-std.org. Retrieved 2018-11-10.
- ^ "Integrating feature-test macros into the C++ WD". www.open-std.org. Retrieved 2018-11-10.
- ^ "P1032R1 - Misc constexpr bits".
- ^ "Smart pointer creation with default initialization".
- ^ "std::map::contains - cppreference.com". en.cppreference.com. Retrieved 2020-05-12.
- ^ a b "P0553R4: Bit operations". www.open-std.org. 2019-07-20. Retrieved 2019-07-20.
- ^ "P0556R3: Integral power-of-2 operations". www.open-std.org. 2018-06-06. Retrieved 2020-08-08.
- ^ "P1355R2 Exposing a narrow contract for ceil2". www.open-std.org. 2019-06-11. Retrieved 2020-08-08.
- ^ "On the names of low-level bit manipulation functions" (PDF). www.open-std.org. 2020-02-11. Retrieved 2020-08-08.
- ^ Tomasz Kamiński (2017-11-09). "Simplified partial function application".
- ^ "C++ keywords - cppreference.com". en.cppreference.com. Retrieved 2019-08-04.
- ^ "explicit specifier - cppreference.com". en.cppreference.com. Retrieved 2019-08-04.
- ^ a b c "P1152R4: Deprecating volatile". www.open-std.org. 2019-07-20. Retrieved 2019-07-20.
- ^ "attribute specifier sequence(since C++11) - cppreference.com". en.cppreference.com. Retrieved 2019-08-04.
- ^ "Working Draft, Standard for Programming Language C++ (Annex C, §C.1)" (PDF).
- ^ a b "P1161R2: Deprecate uses of the comma operator in subscripting expressions". www.open-std.org. Retrieved 2019-07-20.
- ^ "C++ Extensions for Parallelism Version 2".
- ^ "Task Blocks".
- ^ "C++ Extensions for Reflection".
- ^ "C++ Extensions for Networking".
- ^ Sutter, Herb (2019-07-20). "Trip report: Summer ISO C++ standards meeting (Cologne)". Sutter's Mill. Retrieved 2019-07-21.
- ^ "Reflections on the reflection proposals - Meeting C++". meetingcpp.com. Retrieved 2017-06-30.
- ^ "Static reflection". www.open-std.org. Retrieved 2018-11-10.
- ^ Herb Sutter. "Metaclasses" (PDF).
- ^ "A Unified Executors Proposal for C++". www.open-std.org. Retrieved 2019-02-24.
- ^ "N4771: Working Draft, C++ Extensions for Networking" (PDF).
- ^ "ISO/IEC TS 19216:2018 Programming Languages -- C++ Extensions for Networking".
- ^ "A General Property Customization Mechanism". www.open-std.org. Retrieved 2019-02-24.
- ^ "A Unified Futures Proposal for C++".
- ^ https://clang.llvm.org/cxx_status.html
- ^ https://en.cppreference.com/w/cpp/compiler_support
- ^ https://gcc.gnu.org/legacy-ml/gcc-patches/2017-07/msg01234.html
- ^ https://gcc.gnu.org/projects/cxx-status.html
- ^ jawiddis. "Visual Studio 2019 version 16.10 Release Notes". docs.microsoft.com. Retrieved 2021-07-13.
- ^ "MSVC C++20 and the /std:c++20 Switch". docs.microsoft.com. Retrieved 2021-09-07.
- ^ https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/overview/cpp-conformance-improvements.md
- ^ Herb Sutter. "Trip report: Summer ISO C++ standards meeting (Toronto)".
- ^ Andrew Sutton; Bjarne Stroustrup (2013-02-24). "Concepts Lite: Constraining Templates with Predicates". isocpp.org. isocpp.org. Retrieved 2021-01-13.
- ^ Herb Sutter. "Trip report: Fall ISO C++ standards meeting (Albuquerque)".
- ^ Smith, Richard; Perchik, Dawn; Köppe, Thomas. "N4714 Editors' Report -- Programming Languages -- C++". C++ standards drafts. GitHub. Retrieved 27 December 2018.
- ^ Botond Ballo. "Trip Report: C++ Standards Meeting in Jacksonville, March 2018".
- ^ Herb Sutter. "Trip report: Summer ISO C++ standards meeting (Rapperswil)".
- ^ "Support for contract based programming in C++". www.open-std.org. Retrieved 2018-11-10.
- ^ "P1289R0 - Access control in contract conditions" (PDF).
- ^ "r/cpp - 2019-02 Kona ISO C++ Committee Trip Report (C++20 design is complete; Modules in C++20; Coroutines in C++20; Reflection TS v1 published; work begins on a C++ Ecosystem Technical Report)". reddit. Retrieved 2019-02-24.
- ^ "Trip report: Winter ISO C++ standards meeting (Kona)". Sutter's Mill. 2019-02-23. Retrieved 2019-02-24.
- ^ "r/cpp - 2019-07 Cologne ISO C++ Committee Trip Report —