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
thisobject parameter if consteval- multidimensional subscript operator
- static call and subscript operators and static lambdas
- simplifying implicit move
autoandauto- new preprocessor directives:
- *
#elifdefand#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_tand the corresponding signed type: - extended floating-point types with literals
- optional
from nullary lambda expressions - attributes on lambda expressions
constexprchanges:- *non-literal variables, labels, and
gotos inconstexprfunctions - *allowing
staticandthread_localvariables that are usable in constant expressions inconstexprfunctions - *
constexprfunction does not need its return type and parameter types to be literal type - *it is now possible to write a
constexprfunction for which no invocation satisfies the requirements of a core constant expression - narrowing contextual conversions to
boolinstatic_assertandif 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
stdandstd.compatCoroutine Library Support
- synchronous coroutine
std::generatorfor rangesGeneral Utilities Support
- result type
std::expected - monadic operations for
std::optionalandstd::expected - utility function
std::to_underlyingto get the underlying value of enum - move-only callable wrapper
std::move_only_function std::forward_likestd::invoke_rstd::bind_backstd::byteswapstd::unreachable: a function to mark unreachable code- made
std::tuplecompatible with other tuple-like objects std::basic_common_referencespecialization forstd::reference_wrapperyielding reference types- adding default arguments for
std::pair's forwarding constructorCompile-time Support
constexprsupport for:- *
std::type_info::operator - *
std::bitset - *
std::unique_ptr - * for some
functions - * for integral overloads of
std::to_charsandstd::from_chars - metaprogramming utilities:
- *type traits
std::is_scoped_enum,std::is_implicit_lifetime,std::reference_constructs_from_temporary, andstd::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_lastand other variants - *rangified versions of
iota,shift_left, andshift_right - *range fold algorithms
- new
std::ranges::range_adaptor_closure, a helper for defining user-defined range adaptor closures - new range adaptors:
- *
std::views::zipand other variants - *
std::views::adjacentand 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::cbeginand 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
explicitMemory Management Support
std::out_ptrandstd::inout_ptrfor C interoperabilitystd::allocate_at_leastandstd::allocator::allocate_at_least- explicit lifetime management function
std::start_lifetime_asfor implicit-lifetime types - disallowing user specialization of
std::allocator_traitsString and Text Processing Support
- new member functions and changes in string types:
- *
std::basic_string_view::containsandstd::basic_string::contains - *disabling construction from
nullptrforstd::basic_stringandstd::basic_string_view - *explicit range constructor for
std::basic_string_view - *
std::basic_string::resize_and_overwrite - *rvalue reference overload of
std::basic_string::substrfor efficient slicing - formatting ranges, tuples, escaped presentation of characters and strings,
std::thread::id, and stacktraces.Diagnostic Support
- stacktrace library, header
and classstd::stacktraceI/O Support
- formatted output functions
std::printandstd::printlnfrom new header - spanstream library from new header
- a support for exclusive mode in
std::fstreamsContainers 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::spanandstd::basic_string_viewto be trivially copyableC-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_tto match existing practices - allowing some pointers and references of
thisor unknown origin in constant expressions - introduction of immediate-escalating functions promoted to immediate functions
- allowing
static_assertin uninstantiated template contextsLibrary 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_viewtostd::ranges::lazy_split_viewand newsplit_view - *removed
std::default_initializableconstraint from conceptstd::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
constexprstd::variantandstd::optional - supporting types derived from
std::variantinstd::visitRemoved features and deprecation
- 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.
std::aligned_storageandstd::aligned_unionstd::numeric_limits::has_denorm
- 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