Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

What is new in C++23

A tour of C++23: std::print, std::expected, explicit object parameters, if consteval, std::generator, ranges::to, flat_map, mdspan and other additions.

C++23 is a refinement release. It finishes work started in C++20 (more ranges, a standard generator for coroutines, constexpr everywhere), adds a modern output function, a standard way to return errors without exceptions, and a handful of language features that remove long-standing awkwardness. It is the standard that our live examples use.

Compiler support is good for most of it in GCC 14/15, current Clang and current MSVC, with a few library parts arriving later. The status on our runner (GCC 15.2, -std=c++23) is listed below: items marked read-only compile elsewhere but not here.

Terminal
g++ -std=c++23 main.cpp

Try it

C++ print, expected, explicit this and if consteval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Ranges additions, flat_map, generator and more

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Language features

Feature What it gives you
Explicit object parameter (this auto&& self) One member function that works for lvalues, const and rvalues, recursive lambdas, and CRTP without the pattern.
if consteval Different code paths for constant evaluation and run time, without std::is_constant_evaluated().
Multidimensional operator[] m[row, col] instead of m(row, col).
static operator() and static operator[] Function objects and lambdas without a needless this.
auto(x) and auto{x} Explicit decay-copy of a value.
#elifdef, #elifndef, #warning New preprocessor conveniences (#warning was a long-standing extension).
Literal suffixes uz and z std::size_t and ssize_t-like literals: for (auto i = 0uz; ...).
constexpr growth More of the language and library can be used in constant expressions, and static constexpr variables are allowed in constexpr functions.
Names for \N{...} and \u{...} Named and delimited Unicode escapes.
[[assume(expr)]] Tells the optimizer a condition holds (wrong assumptions are undefined behavior).

Library features

  • <print>: std::print and std::println use std::format syntax, are faster than cout, and handle Unicode correctly. See std::print.
  • <expected>: std::expected<T, E> holds a value or an error. Monadic operations (and_then, transform, or_else) work on it and on std::optional. See std::expected.
  • <generator>: std::generator<T> makes coroutine-based lazy sequences. See std::generator.
  • Ranges: ranges::to, views::zip, zip_transform, enumerate, adjacent, pairwise, chunk, slide, chunk_by, join_with, stride, repeat, cartesian_product, as_rvalue, and ranges::fold_left, ranges::find_last, ranges::contains. See C++23 views.
  • Containers: std::flat_map, flat_set, flat_multimap, flat_multiset; std::mdspan (a multidimensional view). See flat_map.
  • Callables: std::move_only_function, std::function_ref arrives in C++26. See move_only_function.
  • Utilities: std::to_underlying, std::unreachable, std::byteswap, std::is_scoped_enum, std::stacktrace, std::spanstream, std::string::contains, std::string::resize_and_overwrite, and formatting of ranges and tuples with std::format.
  • Modules in the library: import std; and import std.compat;.

What does not run on our runner

These compile on newer toolchains, but are shown read-only here (GCC 15.2, -std=c++23, no modules support in the runner):

C++
import std;                              // standard library module: needs build-system support

#include <mdspan>                        // not available in this libstdc++ configuration

#include <stacktrace>                    // GCC needs -lstdc++exp
auto trace = std::stacktrace::current();

Usage notes

  • Prefer std::println to std::cout in new code, unless you must interoperate with streams. It writes one formatted string at once, so output from threads is less likely to interleave.
  • expected is for expected failures such as parsing and validation. Keep exceptions for exceptional ones. See error-handling strategies.
  • flat_map has different performance: fast lookups and iteration, slow insertion in the middle (like a sorted vector), and iterator invalidation on insert.
  • [[assume]] is dangerous. If the condition is false, the behavior is undefined. Use it only for conditions you can prove.
  • A generator must outlive its consumer's use of references it yields, and its elements are computed lazily.
  • Library support varies. Check feature-test macros (__cpp_lib_print, __cpp_lib_expected, ...) when you ship to several compilers (see feature-test macros).