C++23, formally ISO/IEC 14882:2024, is the current open standard for the C++ programming language, published in 2024. It follows C++20, and precedes C++26. The final draft of this version is N4950, which was technically finalized by WG21 in February 2023.
Modern "Hello, world" Example
After many library changes applied to the working draft, the new "Hello, world" program will be: https://en.cppreference.com/w/cpp/io/println
Features
Changes that have been accepted into C++23 include:
Language
- explicit <code>this</code> object parameter
- <code>if consteval</code>
- multidimensional subscript operator
- static call and subscript operators and static lambdas
- simplifying implicit move
- <code>auto(x)</code> and <code>auto{x}</code>
- new preprocessor directives:
- <code>#elifdef</code> and <code>#elifndef</code>
- <code>#warning</code>
- extending the lifetime of some temporaries in range-based for loop
- new standard attribute <code><nowiki>assume(expression)</nowiki></code>
- class template argument deduction from inherited constructors
- labels at the end of the compound statement
- alias declarations in init-statements
- literal suffixes for <code>size_t</code> and the corresponding signed type: (such as <code>0zu</code>, <code>15zu</code>)
- extended floating-point types with literals (conditionally supported)
- optional <code>()</code> from nullary lambda expressions
- attributes on lambda expressions
- <code>constexpr</code> changes:
- non-literal variables, labels, and <code>goto</code>s in <code>constexpr</code> functions
- allowing <code>static</code> and <code>thread_local</code> variables that are usable in constant expressions in <code>constexpr</code> functions
- <code>constexpr</code> function does not need its return type and parameter types to be literal type
- it is now possible to write a <code>constexpr</code> function for which no invocation satisfies the requirements of a core constant expression
- narrowing contextual conversions to <code>bool</code> in <code>static_assert</code> and <code>if constexpr</code>
- trimming whitespaces before line splicing
- make declaration order layout mandated
- delimited escape sequences
- named universal character escapes
- text encoding changes:
- support for UTF-8 as a portable source file encoding
- consistent character literal encoding
- character sets and encodings
- New meaning added to some keywords, such as for <code>this</code>.
Library
Standard Library Module Support
- standard library modules <code>std</code> and <code>std.compat</code>
Coroutine Library Support
- synchronous coroutine <code>std::generator</code> for ranges
General Utilities Support
- result type <code>std::expected</code>
- monadic operations for <code>std::optional</code> and <code>std::expected</code>
- utility function <code>std::to_underlying</code> to get the underlying value of enum
- move-only callable wrapper <code>std::move_only_function</code>
- <code>std::forward_like</code>
- <code>std::invoke_r</code>
- <code>std::bind_back</code>
- <code>std::byteswap</code>
- <code>std::unreachable</code>: a function to mark unreachable code
- made <code>std::tuple</code> compatible with other tuple-like objects
- <code>std::basic_common_reference</code> specialization for <code>std::reference_wrapper</code> yielding reference types
- adding default arguments for <code>std::pair</code>'s forwarding constructor
Compile-time Support
- <code>constexpr</code> support for:
- <code>std::type_info::operator==</code>
- <code>std::bitset</code>
- <code>std::unique_ptr</code>
- for some <code><cmath></code> functions
- for integral overloads of <code>std::to_chars</code> and <code>std::from_chars</code>
- metaprogramming utilities:
- type traits <code>std::is_scoped_enum</code>, <code>std::is_implicit_lifetime</code>, <code>std::reference_constructs_from_temporary</code>, and <code>std::reference_converts_from_temporary</code>.
- adding move-only types support for comparison concepts
Iterators, Ranges, and Algorithm Support
- new range conversion function <code>std::ranges::to</code>
- new constrained ranges algorithm:
- <code>std::ranges::starts_with</code>
- <code>std::ranges::ends_with</code>
- <code>std::ranges::contains</code>
- <code>std::ranges::contains_subrange</code>
- <code>std::ranges::find_last</code> and other variants
- rangified versions of <code>iota</code>, <code>shift_left</code>, and <code>shift_right</code>
- range fold algorithms
- new <code>std::ranges::range_adaptor_closure</code>, a helper for defining user-defined range adaptor closures
- new range adaptors:
- <code>std::views::zip</code> and other variants
- <code>std::views::adjacent</code> and other variants
- <code>std::views::join_with</code>
- <code>std::views::slide</code>
- <code>std::views::chunk</code>
- <code>std::views::chunk_by</code>
- <code>std::views::as_rvalue</code>
- <code>std::views::as_const</code>
- <code>std::views::repeat</code>
- <code>std::views::stride</code>
- <code>std::views::cartesian_product</code>
- <code>std::views::enumerate</code>
- rectifying constant iterators, sentinels, and ranges, that is, <code>std::ranges::cbegin</code> and other similar utilities returning constant iterators should be fully guaranteed even for shallow-const views (such as <code>std::span</code>)
- ranges iterators as inputs to non-ranges algorithms
- relaxing range adaptors to allow for move only types
- making multi-param constructors of some views <code>explicit</code>
Memory Management Support
- <code>std::out_ptr</code> and <code>std::inout_ptr</code> for C interoperability
- <code>std::allocate_at_least</code> and <code>std::allocator::allocate_at_least</code>
- explicit lifetime management function <code>std::start_lifetime_as</code> for implicit-lifetime types
- disallowing user specialization of <code>std::allocator_traits</code>
String and Text Processing Support
- new member functions and changes in string types:
- <code>std::basic_string_view::contains</code> and <code>std::basic_string::contains</code>
- disabling construction from <code>nullptr</code> for <code>std::basic_string</code> and <code>std::basic_string_view</code>
- explicit range constructor for <code>std::basic_string_view</code>
- <code>std::basic_string::resize_and_overwrite</code>
- rvalue reference overload of <code>std::basic_string::substr</code> for efficient slicing
- formatting ranges, tuples, escaped presentation of characters and strings, <code>std::thread::id</code>, and stacktraces.
Diagnostic Support
- stacktrace library, header <code><stacktrace></code> and class <code>std::stacktrace</code>
I/O Support
- formatted output functions <code>std::print</code> and <code>std::println</code> from new header <code><print></code>
- spanstream library (<code>std::span</code>-based string stream) from new header <code><spanstream></code>
- a support for exclusive mode in <code>std::fstream</code>s
Containers Support
- multidimensional-span <code>std::mdspan</code>
- constructability and assignability of containers from other compatible ranges
- flat set and flat map container adapters
- non-deduction context for allocators in container deduction guides
- heterogeneous erasure overloads for associative containers
- allowing iterator pair construction in stack and queue
- requiring <code>std::span</code> and <code>std::basic_string_view</code> to be trivially copyable
C-Compatibility Support
- new header <code><stdatomic.h></code>
Language defect reports
- C++ identifier syntax using Unicode Standard Annex 31
- allowing duplicate attributes
- changing scope of lambda trailing return type
- making overloaded comparison operators less breaking change
- undeprecating volatile compound assignments
- fixing the compatibility and portability of <code>char8_t</code>
- relaxing requirements on <code>wchar_t</code> to match existing practices
- allowing some pointers and references of <code>this</code> or unknown origin in constant expressions
- introduction of immediate-escalating functions promoted to immediate functions
- allowing <code>static_assert(false)</code> in uninstantiated template contexts
Library defect reports
- changes in ranges library:
- conditionally borrowed ranges
- repairing input range adaptors and <code>std::counted_iterator</code>
- relaxing the constraint on <code>std::ranges::join_view</code>
- renamed <code>std::ranges::split_view</code> to <code>std::ranges::lazy_split_view</code> and new <code>split_view</code>
- removed <code>std::default_initializable</code> constraint from concept <code>std::ranges::view</code>
- view with ownership and new <code>std::ranges::owning_view</code>
- fixed <code>std::ranges::istream_view</code>
- changes in text formatting library:
- <code>std::basic_format_string</code>
- compile-time format string checks
- reducing binary code size of <code>std::format_to</code>
- fixing locale handling in chrono formatters
- improving width estimation and fill character allowances of <code>std::format</code>
- use of forwarding references in format arguments to allow non-const-formattable types
- fully <code>constexpr</code> <code>std::variant</code> and <code>std::optional</code>
- supporting types derived from <code>std::variant</code> in <code>std::visit</code>
Removed features and deprecation
Removed features:
- Garbage Collection Support and (strict) Pointer Safety (meaning only relaxed pointer safety is to be relied upon). 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. 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.
- Non-encodable wide character literals and multicharacter wide character literals.
Deprecated features:
- <code>std::aligned_storage</code> and <code>std::aligned_union</code>
- <code>std::numeric_limits::has_denorm</code>
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 <code>operator[]</code>.
- C headers (The corresponding <code><*.h></code> 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 <code>-std=c++23</code>.
- GCC added partial, experimental C++23 support in 2021 in version 11 through the option <code>-std=c++2b</code> or <code>-std=c++23</code> It also has an option to enable GNU extensions in addition to the experimental C++23 support, <code>-std=gnu++2b</code>.
History
In February 2020, at the final meeting for C++20 in Prague, an overall plan for C++23 was adopted: 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, as was the November 2020 meeting in New York and the February 2021 meeting in Kona, Hawaii. All meetings until November 2022 were virtual while the November 2022 meeting until the final meeting in February 2023 was hybrid. The standard was technically finalized by WG21 at the hybrid meeting in Issaquah in February 2023.
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:
- Literal suffixes for <code>std::size_t</code> and the corresponding signed type
- A member function for and , to check whether or not the string contains a given substring or character
- A stacktrace library (), based on Boost.Stacktrace
- A type trait
- The header , for interoperability with C atomics
February 2021
After the virtual WG21 meeting of 22 February 2021, following features are added where they were approved by straw polls:
- Removing unnecessary empty parameter list from lambda expressions.
- Repairing input range adaptors and .
- Relax the requirements for .
- for classes that are derived from .
- .
- Conditionally borrowed ranges.
- .
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:
- Consteval if ().
- Narrowing contextual conversions to .
- Allowing duplicate attributes.
- -based string-stream ().
- and .
- for , , and .
- Iterators pair constructors for (stack) and (queue).
- Few changes of the ranges library:
- Generalized and for arbitrary ranges.
- Renamed to and new .
- Relaxing the constraint on .
- Removing constraint from concept .
- Range constructor for .
- Prohibiting and construction from .
- .
- Improvements on .
- Adding default arguments for 's forwarding constructor.
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:
- Non-literal variables, labels, and gotos in functions, but still ill-formed to evaluate them at compile-time.
- Explicit object parameter.
- Changes on character sets and encodings.
- New preprocessors: and . Both directives were added to C23 (C language update) and GCC 12.
- Allowing alias declarations in init-statement.
- Overloading multidimensional subscript operator (e.g. ).
- Decay copy in language: or .
- Changes in text formatting library:
- Fixing locale handling in chrono formatters.
- Use of forwarding references in format arguments to allow -like types.
- Addition of type alias which is equivalent to .
- Changes in ranges library:
- Refined definition of a view.
- Replacing function template with alias templates , , and customization point object .
- range adaptor family:
- (and being equivalent to )
- (and being equivalent to )
- .
- Monadic operations for .
- Member function template .
- Printing pointers ().
- .
- Heterogeneous erasure overloads for associative containers.
- Every specialization of and is trivially copyable.
- Adding conditional specifications to .
- Revamped specification and use of integer-class types.
- 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."
February 2022
After the virtual WG21 meeting of 7 February 2022, the following features are added where they were approved by straw polls:
- Allowed attributes on the function call operator of a lambda
- for <code>cmath</code> and <code>cstdlib</code>
- Function to mark unreachable code
- A type trait to detect reference binding to temporary
- Making
- Pipe support for user-defined range adaptors
- , and
- Windowing range adaptors: and
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:
- Made rewriting equality in expressions less of a breaking change.
- Reverted the deprecation of bitwise assignment to variables.
- Added the 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 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,
- Allowed and lambdas to be .
- Allowed the <code>this</code> 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 <code>std::float16_t</code>, <code>std::float32_t</code>, <code>std::float64_t</code>, <code>std::float128_t</code>, <code>std::bfloat16_t</code> for these extended types accessible through the header <code><stdfloat></code>, their corresponding literal suffixes <code>f16</code> <code>f32</code> <code>f64</code> <code>f128</code> <code>bf16</code>or <code>F16</code> <code>F32</code> <code>F64</code> <code>F128</code> <code>BF16</code> and added overloads to various standard library functions that take floats as arguments.
- Added the 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 and to be initialized with UTF-8 string literals.
- Removed the requirement that can encode all characters of the extended character set, in effect allowing UTF-16 to be used for wide string literals.
- Added <code>std::mdspan</code>, a multidimensional array view analogous to <code>std::span</code>.
- and were added to the standard library.
- Added the and functions for printing formatted text to stdout.
- Provide the named modules and for importing the standard library.
- Added support for exclusive mode <code>fstreams</code>, analogous to the "x" flag in <code>fopen</code>.
- Allowed <code>std::format</code> to handle ranges, tuples, and other containers.
- Added <code>std::forward_like</code>.
- Made <code>std::string::substr</code> use move semantics.
- Added which implements a coroutine generator that models
- , , , , .
- Added new algorithms: , , and ranges fold algorithms.
- Made <code>std::tuple</code> compatible with other tuple-like objects.
- Explicit lifetime management for implicit-lifetime types.
- Made <code>std::bitset</code> and integral overloads of <code>std::to_chars</code> and <code>std::from_chars</code> -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:
- Allowed to be .
- Allowed and variables to appear in functions if they are usable in constant expressions.
- propagates upwards, that is, certain existing functions become 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 loop to cover the entire loop.
- Reverted the deprecation of (all, not just bitwise) compound assignment to variables.
- Monadic functions for .
- Synchronize the output of with the underlying stream if the native Unicode API is used.
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:
- Referencing the Unicode Standard.
- Stashing stashing iterators for proper flattening.
- <code>views::enumerate</code>
- making multi-param constructors of views explicit
- relaxing ranges just a smidge
- escaping improvements in <code>std::format</code>
- improving <code>std::format</code>'s width estimation
- <code>std::format</code> fill character allowances
- formatting <code>thread::id</code> and <code>stacktrace</code>
- A type trait <code>std::is_implicit_lifetime</code>
- <code>std::common_reference_t</code> of <code>std::reference_wrapper</code> should be a reference type
- disallowing user specialization of <code>std::allocator_traits</code>
- <code>std::pmr::generator</code>
- deprecating <code>std::numeric_limits::has_denorm</code>
- <code>std::barrier</code>'s phase completion guarantees
References
External links