C++23


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:

import std;
int main

Features

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

Language

  • explicit this object parameter
  • if consteval
  • multidimensional subscript operator
  • static call and subscript operators and static lambdas
  • simplifying implicit move
  • auto and auto
  • new preprocessor directives:
  • *#elifdef and #elifndef
  • *#warning
  • extending the lifetime of some temporaries in range-based for loop
  • new standard attribute assume
  • class template argument deduction from inherited constructors
  • labels at the end of the compound statement
  • alias declarations in init-statements
  • literal suffixes for size_t and the corresponding signed type:
  • extended floating-point types with literals
  • optional from nullary lambda expressions
  • attributes on lambda expressions
  • constexpr changes:
  • *non-literal variables, labels, and gotos in constexpr functions
  • *allowing static and thread_local variables that are usable in constant expressions in constexpr functions
  • *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
  • narrowing contextual conversions to bool in static_assert and if constexpr
  • 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 this.

    Library

Standard Library Module Support

  • standard library modules std and std.compat

    Coroutine Library Support

  • synchronous coroutine std::generator for ranges

    General Utilities Support

  • result type std::expected
  • monadic operations for std::optional and std::expected
  • utility function std::to_underlying to get the underlying value of enum
  • move-only callable wrapper std::move_only_function
  • std::forward_like
  • std::invoke_r
  • std::bind_back
  • std::byteswap
  • std::unreachable: a function to mark unreachable code
  • made std::tuple compatible with other tuple-like objects
  • std::basic_common_reference specialization for std::reference_wrapper yielding reference types
  • adding default arguments for std::pair's forwarding constructor

    Compile-time Support

  • constexpr support for:
  • *std::type_info::operator
  • * std::bitset
  • * std::unique_ptr
  • * for some functions
  • * for integral overloads of std::to_chars and std::from_chars
  • metaprogramming utilities:
  • *type traits std::is_scoped_enum, std::is_implicit_lifetime, std::reference_constructs_from_temporary, and std::reference_converts_from_temporary.
  • adding move-only types support for comparison concepts

    Iterators, Ranges, and Algorithm Support

  • new range conversion function std::ranges::to
  • new constrained ranges algorithm:
  • *std::ranges::starts_with
  • *std::ranges::ends_with
  • *std::ranges::contains
  • *std::ranges::contains_subrange
  • *std::ranges::find_last and other variants
  • *rangified versions of iota, shift_left, and shift_right
  • *range fold algorithms
  • new std::ranges::range_adaptor_closure, a helper for defining user-defined range adaptor closures
  • new range adaptors:
  • *std::views::zip and other variants
  • *std::views::adjacent and other variants
  • *std::views::join_with
  • *std::views::slide
  • *std::views::chunk
  • *std::views::chunk_by
  • *std::views::as_rvalue
  • *std::views::as_const
  • *std::views::repeat
  • *std::views::stride
  • *std::views::cartesian_product
  • *std::views::enumerate
  • 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
  • ranges iterators as inputs to non-ranges algorithms
  • relaxing range adaptors to allow for move only types
  • making multi-param constructors of some views explicit

    Memory Management Support

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

    String and Text Processing Support

  • new member functions and changes in string types:
  • *std::basic_string_view::contains and std::basic_string::contains
  • *disabling construction from nullptr for std::basic_string and std::basic_string_view
  • *explicit range constructor for std::basic_string_view
  • *std::basic_string::resize_and_overwrite
  • *rvalue reference overload of std::basic_string::substr for efficient slicing
  • formatting ranges, tuples, escaped presentation of characters and strings, std::thread::id, and stacktraces.

    Diagnostic Support

  • stacktrace library, header and class std::stacktrace

    I/O Support

  • formatted output functions std::print and std::println from new header
  • spanstream library from new header
  • a support for exclusive mode in std::fstreams

    Containers Support

  • multidimensional-span std::mdspan
  • 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 std::span and std::basic_string_view to be trivially copyable

    C-Compatibility Support

  • new header

    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 char8_t
  • relaxing requirements on wchar_t to match existing practices
  • allowing some pointers and references of this or unknown origin in constant expressions
  • introduction of immediate-escalating functions promoted to immediate functions
  • allowing static_assert in uninstantiated template contexts

    Library defect reports

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

    Removed features and deprecation

Removed features:
  • Garbage Collection Support and Pointer Safety. This minimal garbage collection support, 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 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:
  • std::aligned_storage and std::aligned_union
  • std::numeric_limits::has_denorm
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