Английская Википедия:C++20

Материал из Онлайн справочника
Версия от 05:16, 13 февраля 2024; EducationBot (обсуждение | вклад) (Новая страница: «{{Английская Википедия/Панель перехода}} {{Short description|2020 edition of the C++ programming language standard}} {{multiple issues| {{prose|date=December 2020}} {{primary sources|date=December 2020}} {{Cleanup|reason=Article reads like a changelog|date=February 2023}} }} {{C++ language revisions}} '''C++20''' is a version of the ISO/International...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)
Перейти к навигацииПерейти к поиску

Шаблон:Short description

Шаблон:Multiple issues Шаблон: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.[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]

Features

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 for statement[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 typename in certain circumstances[22]
  • new standard attributes [[no_unique_address]],[23] [[likely]] and [[unlikely]][24]
  • conditional explicit, allowing the explicit modifier to be contingent on a boolean expression[25]
  • expanded constexpr: virtual functions,[26] union,[27] try and catch,[28] dynamic_cast and typeid,[29] std::pointer_traits[30]
  • immediate functions using the new consteval keyword[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, static and thread_local storage duration)[34][35]
  • coroutines[36]
  • using on scoped enums[37]
  • constinit keyword[38]

Library

  • ranges (The One Ranges Proposal)[39]
  • std::make_shared and std::allocate_shared for arrays[40]
  • atomic smart pointers (such as std::atomic<shared_ptr<T>> and std::atomic<weak_ptr<T>>)[41]
  • std::to_address to 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 to std::string_view but span can mutate the referenced sequence)[44]
  • std::erase and std::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 than memcpy() 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]
  • bit operations, such as leading/trailing zero/one count,[52] and log2 operations[53][54][55]
  • std::bind_front[56]

New and changed keywords

Many new keywords added (and the new "spaceship operator", operator <=>), such as concept, constinit,[38] consteval, co_await, co_return, co_yield, requires (plus changed meaning for export), and char8_t (for UTF-8 support).[57] And explicit can take an expression since C++20.[58] Most of the uses of the volatile keyword have been deprecated.[59]

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]][60]

Removed and deprecated

Removed features:[61]

  • 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 and std::result_of_t.

Deprecated features:

  • Use of comma operator in subscript expressions has been deprecated[62]
  • (most of) volatile has been deprecated[59]

Published as Technical Specifications

  • Parallelism TS v2[63] (including task blocks[64])
  • Reflection TS v1[65]
  • Networking TS v1[66]

Deferred to a later standard

  • ContractsШаблон:Snd a new study group (SG21) has been formed to work on a new proposal[67]
  • Reflection[68][69]
  • Metaclasses[70]
  • Executors[71]
  • Networking extensions,[72][73] including async, basic I/O services, timers, buffers and buffer-oriented streams, sockets, and Internet protocols (blocked by executors)
  • Properties[74]
  • Extended futures[75]

Compiler support

Full support[76]

  • Visual Studio 2019 supports all C++20 features through its /std:c++latest option, as of version 16.10.0.[77] An option /std:c++20 to enable C++20 mode is added in version 16.11.0.[78][79]

Microsoft's compiler does not just support Windows, also Linux (and e.g. Android and iOS), while it then requires the "Visual C++ for Linux Development extension".[80]

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).[81]
  • EDG started implementing C++20 features in version 5.0 and as of version 6.1 supports most C++20 core language features.[82]
  • GCC added partial, experimental C++20 support in 2017[83] 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.[84]

History

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

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

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

  • 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>> and std::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:[89]

  • 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 to std::string_view but span can mutate the referenced sequence)
  • <version> header

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

  • contracts (later deferred to a later standard)[91]
  • 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:[92]

  • 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)[93] (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:[94][95][96]

  • 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:[97][98][99][100]

  • contracts were removed (see list of features deferred to a later standard)[101]
  • use of comma operator in subscript expressions has been deprecated[62]
  • constexpr additions (trivial default initialization,[102] unevaluated inline-assembly[103])
  • using scoped enums[37]
  • various changes to the spaceship operator[104][105]
  • DR: minor changes to modules[106]
  • constinit keyword
  • changes to concepts (removal of -> Type return-type-requirements[107])
  • (most of) volatile has been deprecated[59]
  • DR: [[nodiscard]] effects on constructors[108]
  • The new standard library concepts will not use PascalCase (rather standard_case, as the rest of the standard library)[109]
  • text formatting (std::format, [110][111] chrono integration,[112] corner case fixes[113])
  • bit operations[52]
  • constexpr INVOKE[114]
  • math constants[115]
  • consistency additions to atomics (std::atomic_ref<T>,[116] std::atomic<std::shared_ptr<T>>[117])
  • add the <=> operator to the standard library[118]
  • header units for the standard library[119]
  • synchronization facilities[120] (merged from: Efficient atomic waiting and semaphores,[121] latches and barriers,[122] Improving atomic_flag,[123] Don't Make C++ Unimplementable On Small CPUs[124])
  • std::source_location[125]
  • constexpr containers (std::string,[126] std::vector[127])
  • std::stop_token and joining thread (std::jthread)[128]

Changes applied during the NB comment resolution in the fall meeting in November 2019 (Belfast) include:[129][130][131][132]

  • 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 from operator==.[133] This allows also for array members in class-type NTTP.
  • Floating-point types,[134] 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[135]
  • std::spans typedef was changed from index_type to size_type to be consistent with the rest of the standard library[136]
  • 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,[137] P1391R4: Range constructor for std::string_view (constructor from iterator-pair of characters),[138] P1394R4: Range constructor for std::span<ref>,[139] P1870R1: forwarding-range<T> is too subtle[140])
  • Initialization for std::atomic<T> has been changed to make it work with default and list initialization,[141] 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
  • Algorithms in <numeric> have been made constexpr
  • Missing feature-test macros for new or changed features of C++20 have been added[142]

References

Шаблон:Reflist

External links

Шаблон:C++ProLang Шаблон:ISO standards

  1. Шаблон:Cite web
  2. Шаблон:Cite web
  3. Шаблон:Cite web
  4. Шаблон:Cite web
  5. Шаблон:Cite web
  6. Шаблон:Cite web
  7. Шаблон:Cite web
  8. Шаблон:Cite web
  9. Шаблон:Cite web
  10. Шаблон:Cite web
  11. Шаблон:Cite web
  12. Шаблон:Cite web
  13. Шаблон:Cite web
  14. Шаблон:Cite web
  15. Шаблон:Cite web
  16. Шаблон:Cite web
  17. 17,0 17,1 17,2 Шаблон:Cite news
  18. Шаблон:Cite web
  19. Шаблон:Cite web
  20. Шаблон:Cite web
  21. Шаблон:Cite web
  22. Шаблон:Cite web
  23. Шаблон:Cite web
  24. Шаблон:Cite web
  25. Шаблон:Cite web
  26. Шаблон:Cite web
  27. Шаблон:Cite web
  28. Шаблон:Cite web
  29. Шаблон:Cite web
  30. Шаблон:Cite web
  31. Шаблон:Cite web
  32. Шаблон:Cite web
  33. Шаблон:Cite web
  34. Шаблон:Cite web
  35. Шаблон:Cite web
  36. Шаблон:Cite web
  37. 37,0 37,1 Шаблон:Cite web
  38. 38,0 38,1 Шаблон:Cite web
  39. Шаблон:Cite web
  40. Шаблон:Cite web
  41. Шаблон:Cite web
  42. Шаблон:Cite web
  43. Шаблон:Cite web
  44. Шаблон:Cite web
  45. Шаблон:Cite web
  46. Шаблон:Cite web
  47. Шаблон:Cite web
  48. Шаблон:Cite web
  49. Шаблон:Cite web
  50. Шаблон:Cite web
  51. Шаблон:Cite web
  52. 52,0 52,1 Шаблон:Cite web
  53. Шаблон:Cite web
  54. Шаблон:Cite web
  55. Шаблон:Cite web
  56. Шаблон:Cite web
  57. Шаблон:Cite web
  58. Шаблон:Cite web
  59. 59,0 59,1 59,2 Шаблон:Cite web
  60. Шаблон:Cite web
  61. Шаблон:Cite web
  62. 62,0 62,1 Шаблон:Cite web
  63. Шаблон:Cite web
  64. Шаблон:Cite web
  65. Шаблон:Cite web
  66. Шаблон:Cite web
  67. Шаблон:Cite web
  68. Шаблон:Cite web
  69. Шаблон:Cite web
  70. Шаблон:Cite web
  71. Шаблон:Cite web
  72. Шаблон:Cite web
  73. Шаблон:Cite web
  74. Шаблон:Cite web
  75. Шаблон:Cite web
  76. Шаблон:Cite web
  77. Шаблон:Cite web
  78. Шаблон:Cite web
  79. Шаблон:Citation
  80. Шаблон:Cite web
  81. Шаблон:Cite web
  82. Шаблон:Cite web
  83. Шаблон:Cite web
  84. Шаблон:Cite web
  85. Шаблон:Cite web
  86. Шаблон:Cite web
  87. Шаблон:Cite web
  88. Шаблон:Cite web
  89. Шаблон:Cite web
  90. Шаблон:Cite web
  91. Шаблон:Cite web
  92. fall meeting in November 2018
  93. Шаблон:Cite web
  94. winter meeting in February 2019
  95. Шаблон:Cite web
  96. Шаблон:Cite web
  97. Шаблон:Cite web
  98. Шаблон:Cite web
  99. Шаблон:Cite web
  100. Шаблон:Cite web
  101. Шаблон:Cite web
  102. Шаблон:Cite web
  103. Шаблон:Cite web
  104. Шаблон:Cite web
  105. Шаблон:Cite web
  106. Шаблон:Cite web
  107. Шаблон:Cite web
  108. Шаблон:Cite web
  109. Шаблон:Cite web
  110. Шаблон:Cite web
  111. Шаблон:Cite web
  112. Шаблон:Cite web
  113. Шаблон:Cite web
  114. Шаблон:Cite web
  115. Шаблон:Cite web
  116. Шаблон:Cite web
  117. Шаблон:Cite web
  118. Шаблон:Cite web
  119. Шаблон:Cite web
  120. Шаблон:Cite web
  121. Шаблон:Cite web
  122. Шаблон:Cite web
  123. Шаблон:Cite web
  124. Шаблон:Cite web
  125. Шаблон:Cite web
  126. Шаблон:Cite web
  127. Шаблон:Cite web
  128. Шаблон:Cite web
  129. fall meeting in November 2019
  130. Шаблон:Cite web
  131. Шаблон:Cite web
  132. Шаблон:Cite web
  133. Шаблон:Cite web
  134. Шаблон:Cite web
  135. Шаблон:Cite web
  136. Шаблон:Cite web
  137. Шаблон:Cite web
  138. Шаблон:Cite web
  139. Шаблон:Cite web
  140. Шаблон:Cite web
  141. Шаблон:Cite web
  142. Шаблон:Cite web