C++23

Computer programming language From Wikipedia, the free encyclopedia

C++23, formally ISO/IEC 14882:2024,[1] is the current open standard for the C++ programming language that follows C++20. The final draft of this version is N4950.[2][3]

In February 2020, at the final meeting for C++20 in Prague, an overall plan for C++23 was adopted:[4][5] 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,[6][7] as was the November 2020 meeting in New York[8][7] and the February 2021 meeting in Kona, Hawaii.[8] All meetings until November 2022 were virtual while the November 2022 meeting until the final meeting in February 2023 was hybrid.[8] The standard was technically finalized by WG21 at the hybrid meeting in Issaquah in February 2023.[9]

Modern "Hello, world" Example

After many library changes applied to the working draft, the new "Hello, world" program will be:

import std;

int main() {
    std::println("Hello, world!");
}

Features

Summarize
Perspective

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

Language

  • explicit this object parameter[10]
  • if consteval[11]
  • multidimensional subscript operator[12]
  • static call and subscript operators and static lambdas[13][14]
  • simplifying implicit move[15]
  • auto(x) and auto{x}[16]
  • new preprocessor directives:
  • extending the lifetime of some temporaries in range-based for loop[19]
  • new standard attribute [[assume(expression)]][20]
  • class template argument deduction from inherited constructors[21]
  • labels at the end of the compound statement[22]
  • alias declarations in init-statements[23]
  • literal suffixes for std::size_t and the corresponding signed type[24]
  • extended floating-point types with literals (conditionally supported)[25]
  • optional () from nullary lambda expressions[26]
  • attributes on lambda expressions[27]
  • constexpr changes:
    • non-literal variables, labels, and gotos in constexpr functions[28]
    • allowing static and thread_local variables that are usable in constant expressions in constexpr functions[29]
    • 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[30]
  • narrowing contextual conversions to bool in static_assert and if constexpr[31]
  • trimming whitespaces before line splicing[32]
  • make declaration order layout mandated[33]
  • delimited escape sequences[34]
  • named universal character escapes[35]
  • text encoding changes:
    • support for UTF-8 as a portable source file encoding[36]
    • consistent character literal encoding[37]
    • character sets and encodings[38]

Library

Standard Library Module Support

Coroutine Library Support

  • synchronous coroutine std::generator for ranges[40]

General Utilities Support

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

Compile-time Support

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

Iterators, Ranges, and Algorithm Support

  • new range conversion function std::ranges::to[63]
  • new constrained ranges algorithm:
    • std::ranges::starts_with
    • std::ranges::ends_with[64]
    • std::ranges::contains
    • std::ranges::contains_subrange[65]
    • std::ranges::find_last and other variants[66]
    • rangified versions of iota, shift_left, and shift_right[67]
    • range fold algorithms[68]
  • new std::ranges::range_adaptor_closure, a helper for defining user-defined range adaptor closures[48]
  • new range adaptors:
    • std::views::zip and other variants
    • std::views::adjacent and other variants[69]
    • std::views::join_with[70]
    • std::views::slide
    • std::views::chunk[71]
    • std::views::chunk_by[72]
    • std::views::as_rvalue[73]
    • std::views::as_const[74]
    • std::views::repeat[75]
    • std::views::stride[76]
    • std::views::cartesian_product[77]
    • std::views::enumerate[78]
  • 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)[74]
  • ranges iterators as inputs to non-ranges algorithms[79]
  • relaxing range adaptors to allow for move only types[80]
  • making multi-param constructors of some views explicit[81]

Memory Management Support

  • std::out_ptr and std::inout_ptr for C interoperability[82]
  • std::allocate_at_least and std::allocator::allocate_at_least[83]
  • explicit lifetime management function std::start_lifetime_as for implicit-lifetime types[84]
  • disallowing user specialization of std::allocator_traits[85]

String and Text Processing Support

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

Diagnostic Support

  • stacktrace library[94]

I/O Support

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

Containers Support

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

C-Compatibility Support

  • new header <stdatomic.h>[109]

Language defect reports

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

Library defect reports

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

Removed features and deprecation

Removed features:

  • Garbage Collection Support and (strict) Pointer Safety[135] (meaning only relaxed pointer safety is to be relied upon[136]). 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.[137] 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.[138]
  • Non-encodable wide character literals and multicharacter wide character literals.[139]

Deprecated features:

  • std::aligned_storage and std::aligned_union[140]
  • std::numeric_limits::has_denorm[141]

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.[143]
  • 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.[144]

History

Summarize
Perspective

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.

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

  • Literal suffixes for std::size_t and the corresponding signed type
  • A member function contains for std::basic_string and std::basic_string_view, to check whether or not the string contains a given substring or character
  • A stacktrace library (<stacktrace>), based on Boost.Stacktrace
  • A type trait std::is_scoped_enum
  • The header <stdatomic.h>, for interoperability with C atomics

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

  • Removing unnecessary empty parameter list () from lambda expressions.
  • Repairing input range adaptors and counted_iterator.
  • Relax the requirements for time_point::clock.[147]
  • std::visit for classes that are derived from std::variant.
  • Locks lock lockables.[148]
  • Conditionally borrowed ranges.
  • std::to_underlying.

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

  • Consteval if (if consteval).
  • Narrowing contextual conversions to bool.
  • Allowing duplicate attributes.
  • std::span-based string-stream (<spanstream>).
  • std::out_ptr() and std::inout_ptr().
  • constexpr for std::optional, std::variant, and std::type_info::operator==.
  • Iterators pair constructors for std::stack (stack) and std::queue (queue).
  • Few changes of the ranges library:
    • Generalized starts_with and ends_with for arbitrary ranges.
    • Renamed split_view to lazy_split_view and new split_view.
    • Relaxing the constraint on join_view.
    • Removing default_initializable constraint from concept view.
    • Range constructor for std::basic_string_view.
  • Prohibiting std::basic_string and std::basic_string_view construction from nullptr.
  • std::invoke_r.
  • Improvements on std::format.
  • Adding default arguments for std::pair's forwarding constructor.

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. arr[1, 2]).
  • 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 std::generator-like types.
  • Addition of type alias std::pmr::stacktrace which is equivalent to std::basic_stacktrace<std::pmr::polymorphic_allocator>.[152]
  • Changes in ranges library:
    • Refined definition of a view.
    • Replacing function template std::ranges::istream_view with alias templates std::ranges::istream_view, std::ranges::wistream_view, and customization point object std::views::istream.
    • zip range adaptor family:
      • zip_view
      • zip_transform_view
      • adjacent_view (and std::views::pairwise being equivalent to std::views::adjacent<2>)
      • adjacent_transform_view (and std::views::pairwise_transform being equivalent to std::views::adjacent_transform<2>)
  • std::move_only_function.
  • Monadic operations for std::optional.
  • Member function template std::basic_string::resize_and_overwrite.
  • Printing volatile pointers (volatile T*).
  • std::byteswap.
  • Heterogeneous erasure overloads for associative containers.
  • Every specialization of std::span and std::basic_string_view is trivially copyable.
  • Adding conditional noexcept specifications to std::exchange.[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]

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

  • Allowed attributes on the function call operator of a lambda
  • std::expected
  • constexpr for cmath and cstdlib
  • Function to mark unreachable code
  • ranges::to
  • A type trait to detect reference binding to temporary
  • Making std::unique_ptr constexpr
  • Pipe support for user-defined range adaptors
  • ranges::iota, ranges::shift_left and ranges::shift_right
  • views::join_with
  • Windowing range adaptors: views::chunk and views::slide
  • views::chunk_by

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 volatile variables.
  • Added the #warning 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 constexpr 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, U'\N{LATIN CAPITAL LETTER A WITH MACRON}' // Equivalent to U'\u0100'
  • Allowed operator() and lambdas to be static.
  • 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 [[assume(expression)]] 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 char and unsigned char to be initialized with UTF-8 string literals.
  • Removed the requirement that wchar_t 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.
  • flat_map and flat_set were added to the standard library.
  • Added the std::print and std::println functions for printing formatted text to stdout.
  • Provide the named modules std and std.compat 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 std::generator which implements a coroutine generator that models std::ranges::input_range
  • views::cartesian_product, views::repeat, views::stride, views::as_const, views::as_rvalue.
  • Added new algorithms: ranges::find_last, ranges::contains, 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 constexpr-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.

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]

  • Allowed operator[] to be static.
  • Allowed static and thread_local variables to appear in constexpr functions if they are usable in constant expressions.
  • consteval propagates upwards, that is, certain existing constexpr functions become consteval functions when those functions can already only be invoked during compile time.
  • Extended the lifetime of temporaries that appear in the for-range-initializer of a range-based for loop to cover the entire loop.
  • Reverted the deprecation of (all, not just bitwise) compound assignment to volatile variables.
  • Monadic functions for std::expected.
  • Synchronize the output of std::print with the underlying stream if the native Unicode API is used.[159]

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

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.