C++23 is Out: Overview of Key Changes in the New Standard
A landmark event has occurred in the world of programming languages — the international C++ standardization committee (ISO/IEC JTC1/SC22/WG21) has officially approved a new version of the language: C++23. This release, succeeding C++20, is not revolutionary, but it contains many long-awaited and useful improvements that will make code cleaner, safer, and more performant. Let's break down the most important innovations.
Main New Features of C++23
1. std::print and std::println — a modern replacement for cout
Perhaps the most noticeable change for everyday programming. The functions std::print and std::println, based on the powerful formatting from std::format (C++20), have been added to the standard library. Now console output is not only more convenient but also significantly faster than using std::cout.
- Example:
std::println("Hello, {}! Answer: {}", name, 42); - Advantages: type safety, readable syntax, Unicode support.
2. deducing this (P0847R7) — explicit reference to this
One of the most powerful innovations for metaprogramming and library creation. Now you can explicitly pass the this object as a parameter of a member function. This solves old problems with lambdas and CRTP (Curiously Recurring Template Pattern).
- Allows writing a single member function that works with both const and non-const objects.
- Simplifies the implementation of the "overloading by qualifiers" pattern.
3. Multidimensional operator[] (P2128R6)
A long-awaited feature — now operator[] can accept multiple arguments. This is critically important for libraries working with multidimensional arrays (e.g., mdspan or custom matrices).
- Before:
matrix(2, 3)ormatrix[{2,3}] - Now:
matrix[2, 3]— natural and concise.
4. std::expected — functional error handling
An alternative to exceptions and return codes. The type std::expected<T, E> contains either a valid value or an error. This is especially valuable in systems where exceptions are prohibited (embedded systems, OS kernels).
5. Improvements to modules and concepts
Modules (C++20) have received fixes and stabilization. They are now better supported by compilers. Concepts have become more flexible, with the ability to use auto in concept parameters being added.
What else is worth noting?
std::mdspan— a multidimensional span for working with arrays.- Fixes in
std::ranges— stabilization and filling gaps in the API. - Attribute
[[assume]]— hints for the compiler for optimization. - Static
operator()calls — the ability