Top Qs
Timeline
Chat
Perspective
C++20
2020 edition of the C++ programming language standard From Wikipedia, the free encyclopedia
Remove ads
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.[1] The standard was technically finalized[2] by WG21 at the meeting in Prague in February 2020,[3] had its final draft version announced in March 2020,[4] was approved on 4 September 2020,[5][6] and published in December 2020.[7]
Remove ads
Features
Summarize
Perspective
C++20 adds more new major features than C++14 or C++17.[8] Changes that have been accepted into C++20 include:[9]
Language
- concepts,[10] with terse syntax[11]
- modules[12]
- designated initializers[13] (based on the C99 feature, and common g++ extension)
[=, this]as a lambda capture[14]- template parameter lists on lambdas[15]
- three-way comparison using the "spaceship operator",
operator <=> - initialization of an additional variable within a range-based
forstatement[16] - lambdas in unevaluated contexts[17][18]
- default constructible and assignable stateless lambdas[17][19]
- allow pack expansions in lambda init-capture[17][20]
- class types in non-type template parameters, also allowing string literals as template parameters[21]
- removing the need for
typenamein certain circumstances[22] - new standard attributes
[[no_unique_address]],[23][[likely]]and[[unlikely]][24] - conditional
explicit, allowing theexplicitmodifier to be contingent on a Boolean expression[25] - expanded
constexpr: virtual functions,[26]union,[27]tryandcatch,[28]dynamic_castandtypeid,[29]std::pointer_traits[30] - immediate functions using the new
constevalkeyword[31] - signed integers are now defined to be represented using two's complement (signed integer overflow remains undefined behavior)[32]
- a revised memory model[33]
- various improvements to structured bindings (interaction with lambda captures,
staticandthread_localstorage duration)[34][35] - coroutines[36]
usingon scopedenums[37]constinitkeyword[38]
Library
- ranges (The One Ranges Proposal)[39]
std::make_sharedandstd::allocate_sharedfor arrays[40]- atomic smart pointers (such as
std::atomic<shared_ptr<T>>andstd::atomic<weak_ptr<T>>)[41] std::to_addressto convert a pointer to a raw pointer[42]- calendar and time-zone additions to
<chrono>[43] std::span, providing a view to a contiguous array (analogous tostd::string_viewbutspancan mutate the referenced sequence)[44]std::eraseandstd::erase_if, simplifying element erasure for most standard containers[45]<version>header[46]std::bit_cast<>for type casting of object representations, with less verbosity thanmemcpy()and more ability to exploit compiler internals[47]- feature test macros[48]
- various constexpr library bits[49]
- smart pointer creation with default initialization[50]
contains-method for associative containers[51]- prefix and suffix checking for strings[52]
- bit operations, such as leading/trailing zero/one count,[53] and log2 operations[54][55][56]
std::bind_front[57]std::atomic::wait,std::atomic::notify_one, andstd::atomic::notify_allwere added, giving the standard library futex-like capabilities[58]
New and changed keywords
Eight new keywords added (plus the new "spaceship operator", operator <=>), such as concept, constinit,[38] consteval, requires (plus changed meaning for export), 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[59] (additionally six previously existing keywords have changed or new meaning added); explicit can take an expression since C++20.[60] Most of the uses of the volatile keyword have been deprecated.[61]
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]][62]
Removed and deprecated
Removed features:[63]
- 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
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.
Deprecated features:
Remove ads
Published as Technical Specifications
Deferred to a later standard
- Contracts – a new study group (SG21) has been formed to work on a new proposal[68]
- Reflection[69][70]
- Metaclasses[71]
- Executors[72]
- Networking extensions,[73][74] including async, basic I/O services, timers, buffers and buffer-oriented streams, sockets, and Internet protocols (blocked by executors)
- Properties[75]
- Extended futures[76]
Compiler support
Full support[77]
- Visual Studio 2019 supports all C++20 features through its
/std:c++latestoption, as of version 16.10.0.[78] An option/std:c++20to enable C++20 mode is added in version 16.11.0.[79][80]
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.[81]
Partial
- 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).[82] - EDG started implementing C++20 features in version 5.0 and as of version 6.1 supports most C++20 core language features.[83]
- GCC added partial, experimental C++20 support in 2017[84] 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.[85]
Remove ads
History
Summarize
Perspective
Changes applied to the C++20 working draft in July 2017 (Toronto) include:[86]
- concepts (what made it into the standard is a cut-down version; also described as "Concepts Lite"[87])
- designated initializers
[=, this]as a lambda capture- template parameter lists on lambdas
std::make_sharedandstd::allocate_sharedfor arrays
Changes applied to the C++20 working draft in the fall meeting in November 2017 (Albuquerque) include:[88][89]
- 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 (such as
std::atomic<shared_ptr<T>>andstd::atomic<weak_ptr<T>>) std::to_addressto convert a pointer to a raw pointer
Changes applied to the C++20 working draft in March 2018 (Jacksonville) include:[90]
- removing the need for
typenamein 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_viewbutspancan mutate the referenced sequence)<version>header
Changes applied to the C++20 working draft in the summer meeting in June 2018 (Rapperswil) include:[91]
contracts(later deferred to a later standard)[92]- 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:[93]
- ranges (The One Ranges Proposal)
- 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 (signed integer overflow remains undefined behavior)
refinements of the contracts facility (access control in contract conditions)[94] (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:[95][96][97]
- coroutines
- modules
- various improvements to structured bindings (interaction with lambda captures,
staticandthread_localstorage duration)
Changes applied to the C++20 working draft in the summer meeting in July 2019 (Cologne) include:[98][99][100][101]
- contracts were removed (see list of features deferred to a later standard)[102]
- use of comma operator in subscript expressions has been deprecated[64]
constexpradditions (trivial default initialization,[103] unevaluated inline-assembly[104])- using scoped
enums[37] - various changes to the spaceship operator[105][106]
- DR: minor changes to modules[107]
constinitkeyword- changes to concepts (removal of
-> Typereturn-type-requirements[108]) - (most of)
volatilehas been deprecated[61] - DR:
[[nodiscard]]effects on constructors[109] - The new standard library concepts will not use PascalCase (rather
standard_case, as the rest of the standard library)[110] - text formatting (
std::format,[111][112]chronointegration,[113] corner case fixes[114]) - bit operations[53]
constexpr INVOKE[115]- math constants[116]
- consistency additions to atomics (
std::atomic_ref<T>,[117]std::atomic<std::shared_ptr<T>>[118]) - add the
<=>operator to the standard library[119] - header units for the standard library[120]
- synchronization facilities[121] (merged from: Efficient atomic waiting and semaphores,[122] latches and barriers,[123] Improving
atomic_flag,[124] Don't Make C++ Unimplementable On Small CPUs[125]) std::source_location[126]constexprcontainers (std::string,[127]std::vector[128])std::stop_tokenand joining thread (std::jthread)[129]
Changes applied during the NB comment resolution in the fall meeting in November 2019 (Belfast) include:[130][131][132][133]
- 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==.[134] This allows also for array members in class-type NTTP. - Floating-point types,[135] 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[136]std::spans typedef was changed fromindex_typetosize_typeto be consistent with the rest of the standard library[137]- 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,[138] P1391R4: Range constructor for
std::string_view(constructor from iterator-pair of characters),[139] P1394R4: Range constructor forstd::span<ref>,[140] P1870R1: forwarding-range<T> is too subtle[141]) - Initialization for
std::atomic<T>has been changed to make it work with default and list initialization,[142]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[143]
Remove ads
References
External links
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads