C++23

From testwiki
Jump to navigation Jump to search

Template:Short description Template:Update

Template:C++ language revisions C++23, formally ISO/IEC 14882:2024,[1] is a version standard for the C++ programming language, published in 2024. It follows C++20, and was replaced by C++26 in March 2026. The final draft of this version is N4950,[2][3] which was technically finalized by WG21 in February 2023.[4]

Features

Changes that have been accepted into C++23 include:

Language

  • explicit this object parameter[5]
  • if consteval[6]
  • multidimensional subscript operator;[7] this operator may be defined with any number of parameters (or even be variadic), allowing for defining a[i, j, k] = x; instead of a[i][j][k] = x;
  • static call and subscript operators and static lambdas[8][9]
  • simplifying implicit move[10]
  • auto(x) and auto{x}[11]
  • new preprocessor directives:
  • extending the lifetime of some temporaries in range-based for loop[14]
  • new standard attribute [[assume(expression)]][15]
  • class template argument deduction from inherited constructors[16]
  • labels at the end of compound statements[17]
  • alias declarations in init-statements[18]
  • literal suffixes for size_t and the corresponding signed type: Template:Mono (such as 0zu, 15zu)[19]
  • extended floating-point types with literals (conditionally supported)[20]
  • optional () from nullary lambda expressions[21]
  • attributes on lambda expressions[22]
  • constexpr changes:
    • non-literal variables, labels, and gotos in constexpr functions[23]
    • allowing static and thread_local variables that are usable in constant expressions in constexpr functions[24]
    • constexpr function does not need its return type and parameter types to be literal type
    • it is now possible to write a constexpr function for which no invocation satisfies the requirements of a core constant expression[25]
  • narrowing contextual conversions to bool in static_assert and if constexpr[26]
  • trimming whitespaces before line splicing[27]
  • make declaration order layout mandated[28]
  • delimited escape sequences[29]
  • named universal character escapes[30]
  • text encoding changes:
    • support for UTF-8 as a portable source file encoding[31]
    • consistent character literal encoding[32]
    • character sets and encodings[33]
  • New meaning added to some keywords, such as for this.[34]

Library

Standard Library Module Support

Coroutine Library Support

  • synchronous coroutine std::generator<Ref, V, Alloc> for ranges[36]

General Utilities Support

  • result type std::expected<T, E>[37]
  • monadic operations for std::optional<T>[38] and std::expected[39]
  • utility function std::to_underlying to get the underlying value of enum[40]
  • move-only callable wrapper std::move_only_function[41]
  • std::forward_like[42]
  • std::invoke_r[43]
  • std::bind_back[44]
  • std::byteswap[45]
  • std::unreachable: a function to mark unreachable code[46]
  • made std::tuple compatible with other tuple-like objects[47]
  • std::basic_common_reference specialization for std::reference_wrapper yielding reference types[48]
  • adding default arguments for std::pair<T, U>'s forwarding constructor[14]

Compile-time Support

  • constexpr support for:
    • std::type_info::operator==[49]
    • std::bitset[50]
    • std::unique_ptr[51]
    • for some <cmath> functions[52]
    • for integral overloads of std::to_chars and std::from_chars[53]
  • metaprogramming utilities:
    • type traits std::is_scoped_enum,[54] std::is_implicit_lifetime,[55] std::reference_constructs_from_temporary, and std::reference_converts_from_temporary.[56]
  • adding move-only types support for comparison concepts[57]

Iterators, Ranges, and Algorithm Support

  • new range conversion function std::ranges::to[58]
  • new constrained ranges algorithm:
    • std::ranges::starts_with
    • std::ranges::ends_with[59]
    • std::ranges::contains
    • std::ranges::contains_subrange[60]
    • std::ranges::find_last and other variants[61]
    • rangified versions of iota, shift_left, and shift_right[62]
    • range fold algorithms[63]
  • new std::ranges::range_adaptor_closure, a helper for defining user-defined range adaptor closures[44]
  • new range adaptors:
    • std::views::zip and other variants
    • std::views::adjacent and other variants[64]
    • std::views::join_with[65]
    • std::views::slide
    • std::views::chunk[66]
    • std::views::chunk_by[67]
    • std::views::as_rvalue[68]
    • std::views::as_const[69]
    • std::views::repeat[70]
    • std::views::stride[71]
    • std::views::cartesian_product[72]
    • std::views::enumerate[73]
  • rectifying constant iterators, sentinels, and ranges, that is, std::ranges::cbegin and other similar utilities returning constant iterators should be fully guaranteed even for shallow-const views (such as std::span)[69]
  • ranges iterators as inputs to non-ranges algorithms[74]
  • relaxing range adaptors to allow for move only types[75]
  • making multi-param constructors of some views explicit[76]

Memory Management Support

  • std::out_ptr and std::inout_ptr for C interoperability[77]
  • std::allocate_at_least and std::allocator::allocate_at_least[78]
  • explicit lifetime management function std::start_lifetime_as for implicit-lifetime types[79]
  • disallowing user specialization of std::allocator_traits[80]

String and Text Processing Support

  • new member functions and changes in string types:
    • std::basic_string_view::contains and std::basic_string::contains[81]
    • disabling construction from nullptr for std::basic_string and std::basic_string_view[82]
    • explicit range constructor for std::basic_string_view[83]
    • std::basic_string::resize_and_overwrite[84]
    • rvalue reference overload of std::basic_string::substr for efficient slicing[85]
  • formatting ranges, tuples, escaped presentation of characters and strings, std::thread::id, and stacktraces.[86][87][88]

Diagnostic Support

  • stacktrace library, header <stacktrace> and class std::stacktrace[89]

I/O Support

  • formatted output functions std::print and std::println from new header <print>[90]
  • spanstream library (std::span-based string stream) from new header <spanstream>[91]
  • a support for exclusive mode in std::fstreams[92]
  • std::basic_ostream::operator<<(const volatile void* p)[93]

Containers Support

  • multidimensional-span std::mdspan[94][95][96][97]
  • constructability and assignability of containers from other compatible ranges[58]
  • flat set and flat map container adapters[98][99]
  • non-deduction context for allocators in container deduction guides[100]
  • heterogeneous erasure overloads for associative containers[101]
  • allowing iterator pair construction in stack and queue[102]
  • requiring std::span and std::basic_string_view to be trivially copyable[103]

C-Compatibility Support

  • new header <stdatomic.h>[104]

Language defect reports

  • C++ identifier syntax using Unicode Standard Annex 31[105]
  • allowing duplicate attributes[106]
  • changing scope of lambda trailing return type[107]
  • making overloaded comparison operators less breaking change[108]
  • undeprecating volatile compound assignments[109][110]
  • fixing the compatibility and portability of char8_t[111]
  • relaxing requirements on wchar_t to match existing practices[112]
  • allowing some pointers and references of this or unknown origin in constant expressions[113]
  • introduction of immediate-escalating functions promoted to immediate functions[114]
  • allowing static_assert(false) in uninstantiated template contexts

Library defect reports

  • changes in ranges library:
    • conditionally borrowed ranges[115]
    • repairing input range adaptors and std::counted_iterator[116]
    • relaxing the constraint on std::ranges::join_view[117]
    • renamed std::ranges::split_view to std::ranges::lazy_split_view and new split_view[118]
    • removed std::default_initializable constraint from concept std::ranges::view[119]
    • view with ownership and new std::ranges::owning_view[120]
    • fixed std::ranges::istream_view[121]
  • changes in text formatting library:
    • std::basic_format_string[122]
    • compile-time format string checks
    • reducing binary code size of std::format_to[123]
    • fixing locale handling in chrono formatters[124]
    • improving width estimation[125] and fill character allowances of std::format[126]
    • use of forwarding references in format arguments to allow non-const-formattable types[127]
  • fully constexpr std::variant and std::optional[128]
  • supporting types derived from std::variant in std::visit[129]

Removed features and deprecation

Removed features:

  • Garbage Collection Support and (strict) Pointer Safety[130] (meaning only relaxed pointer safety is to be relied upon[131]). This minimal garbage collection support (and pointer safety, needed for it), was added to C++11 but no compilers have ever supported it so the support was removed in C++23.[132] However, that doesn't mean many GC implementations haven't been used, and continue to be used with C++, such as Boehm GC (and it can also just be used for leak detection in leak detection, when in debug mode), and such GC is often implemented in C++, for other languages to use.
  • Mixed wide-string literal concatenation.[133]
  • Non-encodable wide character literals and multicharacter wide character literals.[134]

Deprecated features:

  • std::aligned_storage and std::aligned_union[135]
  • std::numeric_limits::has_denorm[136]

Reverted deprecated features:

  • Use of comma operator in subscript expressions was no longer deprecated but the semantics has been changed to support overloadable n-adic operator[].
  • C headers (The corresponding <*.h> headers for compatibility with C)

Published as Technical Specifications

Compiler support

  • Clang progressively added partial C++23 support from 2021 in version 13 through to version 18 in 2024, available through the option -std=c++23.[138]
  • GCC added partial, experimental C++23 support in 2021 in version 11 through the option -std=c++2b or -std=c++23 It also has an option to enable GNU extensions in addition to the experimental C++23 support, -std=gnu++2b.[139]

History

In February 2020, at the final meeting for C++20 in Prague, an overall plan for C++23 was adopted:[140][141] planned features for C++23 were library support for coroutines, a modular standard library, executors, and networking.

The first WG21 meeting focused on C++23 was intended to take place in Varna in early June 2020, but was cancelled due to the COVID-19 pandemic,[142][143] as was the November 2020 meeting in New York[144][143] and the February 2021 meeting in Kona, Hawaii.[144] All meetings until November 2022 were virtual while the November 2022 meeting until the final meeting in February 2023 was hybrid.[144] The standard was technically finalized by WG21 at the hybrid meeting in Issaquah in February 2023.[4]

In the absence of face-to-face WG21 meetings, the following changes were applied after several virtual WG21 meetings, where they were approved by straw polls.

November 2020

The following were added after the virtual WG21 meeting of 9 November 2020, where they were approved by straw polls:[145]

February 2021

After the virtual WG21 meeting of 22 February 2021, following features are added where they were approved by straw polls:[146]

June 2021

After the summer 2021 ISO C++ standards plenary virtual meeting of June 2021, new features and defect reports were approved by straw polls:[149]

October 2021

After the autumn 2021 ISO C++ standards plenary virtual meeting of October 2021, new features and defect reports were approved by straw polls:[150]

  • Non-literal variables, labels, and gotos in constexpr functions, but still ill-formed to evaluate them at compile-time.
  • Explicit this object parameter.
  • Changes on character sets and encodings.
  • New preprocessors: #elifdef and #elifndef. Both directives were added to C23 (C language update) and GCC 12.[151]
  • Allowing alias declarations in init-statement.
  • Overloading multidimensional subscript operator (e.g. Template:Cpp).
  • Decay copy in language: auto(x) or auto{x}.
  • Changes in text formatting library:
    • Fixing locale handling in chrono formatters.
    • Use of forwarding references in format arguments to allow Template:Cpp-like types.
  • Addition of type alias Template:Cpp which is equivalent to Template:Cpp.[152]
  • Changes in ranges library:
  • Template:Cpp.
  • Monadic operations for Template:Cpp.
  • Member function template Template:Cpp.
  • Printing volatile pointers (volatile T*).
  • Template:Cpp.
  • Heterogeneous erasure overloads for associative containers.
  • Every specialization of Template:Cpp and Template:Cpp is trivially copyable.
  • Adding conditional noexcept specifications to Template:Cpp.[153]
  • Revamped specification and use of integer-class types.[154]
  • Clarify C headers. "The headers are not useful in code that is only required to be valid C++. Therefore, the C headers should be provided by the C++ standard library as a fully-supported, not deprecated part, but they should also be discouraged for use in code that is not polyglot interoperability code. [..] This proposal makes the C headers no longer deprecated, so there is no formal threat of future removal. The effective discouragement to use the C headers in pure C++ code is now spelled out explicitly as normative discouragement."[155]

February 2022

After the virtual WG21 meeting of 7 February 2022, the following features are added where they were approved by straw polls:[156]

July 2022

After the virtual WG21 meeting of 25 July 2022, the following features and defect reports are added where they were approved by straw polls:[157]

  • Made rewriting equality in expressions less of a breaking change.
  • Reverted the deprecation of bitwise assignment to Template:Cpp variables.
  • Added the Template:Cpp preprocessor directive.
  • Removed non-encodable wide character literals and multicharacter wide character literals.
  • Allowed labels to appear at the end of compound statements.
  • Added escape sequences delimited with curly braces for octal and hexadecimal numbers and universal character names.
  • Allowed Template:Cpp functions to never be constant expressions.
  • Simplified some implicit move rules from C++20 and allowed implicit move when returning an rvalue reference.
  • Add a way to specify unicode characters by name. For example, Template:Cpp
  • Allowed [[operators in C and C++|Template:Code]] and lambdas to be [[static (keyword)|Template:Code]].
  • Allowed the this pointer and references of unknown origin to appear in constant expressions.
  • Allowed implementations to define extended floating-point types in addition to the three standard floating-point types. Added the type aliases std::float16_t, std::float32_t, std::float64_t, std::float128_t, std::bfloat16_t for these extended types accessible through the header <stdfloat>, their corresponding literal suffixes f16 f32 f64 f128 bf16or F16 F32 F64 F128 BF16 and added overloads to various standard library functions that take floats as arguments.
  • Added the Template:Cpp attribute which allows the compiler to assume the provided expression is true to allow optimizations.
  • Made support for UTF-8 source files mandatory, providing a portable encoding for source files.
  • Allowed arrays of Template:Cpp and Template:Cpp to be initialized with UTF-8 string literals.
  • Removed the requirement that [[wchar_t|Template:Code]] can encode all characters of the extended character set, in effect allowing UTF-16 to be used for wide string literals.
  • Added std::mdspan, a multidimensional array view analogous to std::span.
  • Template:Cpp and Template:Cpp were added to the standard library.
  • Added the Template:Cpp and Template:Cpp functions for printing formatted text to stdout.
  • Provide the named modules Template:Cpp and Template:Cpp for importing the standard library.
  • Added support for exclusive mode fstreams, analogous to the "x" flag in fopen.
  • Allowed std::format to handle ranges, tuples, and other containers.
  • Added std::forward_like.
  • Made std::string::substr use move semantics.
  • Added Template:Cpp which implements a coroutine generator that models Template:Cpp
  • Template:Cpp, Template:Cpp, Template:Cpp, Template:Cpp, Template:Cpp.
  • Added new algorithms: Template:Cpp, Template:Cpp, and ranges fold algorithms.
  • Made std::tuple compatible with other tuple-like objects.
  • Explicit lifetime management for implicit-lifetime types.
  • Made std::bitset and integral overloads of std::to_chars and std::from_chars Template:Cpp-compatible.
  • Adding move-only types support for comparison concepts.
  • Ranges iterators as inputs to non-ranges algorithms.
  • Relaxing range adaptors to allow for move-only types.

November 2022

After the hybrid WG21 meeting of 7 November 2022, the following features and defect reports are added where they were approved by straw polls:[158]

February 2023

After the final hybrid WG21 meeting of 6-11 February 2023, the following features and defect reports are added where they were approved by straw polls:[160]

  • Referencing the Unicode Standard.[161]
  • Stashing stashing iterators for proper flattening.[162]
  • views::enumerate
  • making multi-param constructors of views explicit
  • relaxing ranges just a smidge
  • escaping improvements in std::format
  • improving std::format's width estimation
  • std::format fill character allowances
  • formatting thread::id and stacktrace
  • A type trait std::is_implicit_lifetime
  • std::common_reference_t of std::reference_wrapper should be a reference type
  • disallowing user specialization of std::allocator_traits
  • std::pmr::generator
  • deprecating std::numeric_limits::has_denorm
  • std::barrier's phase completion guarantees

References

Template:Reflist

Template:C++ programming language

  1. ↑ Template:Cite web
  2. ↑ Template:Cite web
  3. ↑ Template:Cite web
  4. ↑ 4.0 4.1 Template:Cite web
  5. ↑ Template:Cite web
  6. ↑ Template:Cite web
  7. ↑ Template:Cite web
  8. ↑ Template:Cite web
  9. ↑ Template:Cite web
  10. ↑ Template:Cite web
  11. ↑ Template:Cite web
  12. ↑ Template:Cite web
  13. ↑ Template:Cite web
  14. ↑ 14.0 14.1 Template:Cite web
  15. ↑ Template:Cite web
  16. ↑ Template:Cite web
  17. ↑ Template:Cite web
  18. ↑ Template:Cite web
  19. ↑ Template:Cite web
  20. ↑ Template:Cite web
  21. ↑ Template:Cite web
  22. ↑ Template:Cite web
  23. ↑ Template:Cite web
  24. ↑ Template:Cite web
  25. ↑ Template:Cite web
  26. ↑ Template:Cite web
  27. ↑ Template:Cite web
  28. ↑ Template:Cite web
  29. ↑ Template:Cite web
  30. ↑ Template:Cite web
  31. ↑ Template:Cite web
  32. ↑ Template:Cite web
  33. ↑ Template:Cite web
  34. ↑ Template:Cite web
  35. ↑ Template:Cite web
  36. ↑ Template:Cite web
  37. ↑ Template:Cite web
  38. ↑ Template:Cite web
  39. ↑ Template:Cite web
  40. ↑ Template:Cite web
  41. ↑ Template:Cite web
  42. ↑ Template:Cite web
  43. ↑ Template:Cite web
  44. ↑ 44.0 44.1 Template:Cite web
  45. ↑ Template:Cite web
  46. ↑ Template:Cite web
  47. ↑ Template:Cite web
  48. ↑ Template:Cite web
  49. ↑ Template:Cite web
  50. ↑ Template:Cite web
  51. ↑ Template:Cite web
  52. ↑ Template:Cite web
  53. ↑ Template:Cite web
  54. ↑ Template:Cite web
  55. ↑ Template:Cite web
  56. ↑ Template:Cite web
  57. ↑ Template:Cite web
  58. ↑ 58.0 58.1 Template:Cite web
  59. ↑ Template:Cite web
  60. ↑ Template:Cite web
  61. ↑ Template:Cite web
  62. ↑ Template:Cite web
  63. ↑ Template:Cite web
  64. ↑ Template:Cite web
  65. ↑ Template:Cite web
  66. ↑ Template:Cite web
  67. ↑ Template:Cite web
  68. ↑ Template:Cite web
  69. ↑ 69.0 69.1 Template:Cite web
  70. ↑ Template:Cite web
  71. ↑ Template:Cite web
  72. ↑ Template:Cite web
  73. ↑ Template:Cite web
  74. ↑ Template:Cite web
  75. ↑ Template:Cite web
  76. ↑ Template:Cite web
  77. ↑ Template:Cite web
  78. ↑ Template:Cite web
  79. ↑ Template:Cite web
  80. ↑ Template:Cite web
  81. ↑ Template:Cite web
  82. ↑ Template:Cite web
  83. ↑ Template:Cite web
  84. ↑ Template:Cite web
  85. ↑ Template:Cite web
  86. ↑ Template:Cite web
  87. ↑ Template:Cite web
  88. ↑ Template:Cite web
  89. ↑ Template:Cite web
  90. ↑ Template:Cite web
  91. ↑ Template:Cite web
  92. ↑ Template:Cite web
  93. ↑ Template:Cite web
  94. ↑ Template:Cite web
  95. ↑ Template:Cite web
  96. ↑ Template:Cite web
  97. ↑ Template:Cite web
  98. ↑ Template:Cite web
  99. ↑ Template:Cite web
  100. ↑ Template:Cite web
  101. ↑ Template:Cite web
  102. ↑ Template:Cite web
  103. ↑ Template:Cite web
  104. ↑ Template:Cite web
  105. ↑ Template:Cite web
  106. ↑ Template:Cite web
  107. ↑ Template:Cite web
  108. ↑ Template:Cite web
  109. ↑ Template:Cite web
  110. ↑ Template:Cite web
  111. ↑ Template:Cite web
  112. ↑ Template:Cite web
  113. ↑ Template:Cite web
  114. ↑ Template:Cite web
  115. ↑ Template:Cite web
  116. ↑ Template:Cite web
  117. ↑ Template:Cite web
  118. ↑ Template:Cite web
  119. ↑ Template:Cite web
  120. ↑ Template:Cite web
  121. ↑ Template:Cite web
  122. ↑ Template:Cite web
  123. ↑ Template:Cite web
  124. ↑ Template:Cite web
  125. ↑ Template:Cite web
  126. ↑ Template:Cite web
  127. ↑ Template:Cite web
  128. ↑ Template:Cite web
  129. ↑ Template:Cite web
  130. ↑ Template:Cite web
  131. ↑ Template:Cite web
  132. ↑ Template:Cite web
  133. ↑ Template:Cite web
  134. ↑ Template:Cite web
  135. ↑ Template:Cite web
  136. ↑ Template:Cite web
  137. ↑ Template:Cite web
  138. ↑ Template:Cite web
  139. ↑ Template:Cite web
  140. ↑ Template:Cite web
  141. ↑ Template:Cite web
  142. ↑ Template:Cite web
  143. ↑ 143.0 143.1 Template:Cite web
  144. ↑ 144.0 144.1 144.2 Template:Cite web
  145. ↑ Template:Cite web
  146. ↑ Template:Cite web
  147. ↑ Template:Cite web
  148. ↑ Template:Cite web
  149. ↑ Template:Cite web
  150. ↑ Template:Cite web
  151. ↑ Template:Cite web
  152. ↑ Template:Cite web
  153. ↑ Template:Cite web
  154. ↑ Template:Cite web
  155. ↑ Template:Cite web
  156. ↑ Template:Cite web
  157. ↑ Template:Cite web
  158. ↑ Template:Cite web
  159. ↑ Template:Cite web
  160. ↑ Template:Cite web
  161. ↑ Template:Cite web
  162. ↑ Template:Cite web