📚

Highlights of New Features in C++23

Feb 28, 2025

What's New in C++23

Introduction

  • Presenter: Software architect and project manager at Nikon Metrology (Belgium)
  • Author of several books, including Professional C++ (5th edition)
  • Founder of the Belgian C++ Users Group, organizes events
  • Overview of C++23 features, focusing on high-level insights

Agenda

  • Overview of new language features
  • Overview of new standard library features
  • Questions will be held at the end of the session

Core Language Features

Explicit Object Parameters

  • Replaces implicit this pointer in member functions
  • Allows ref-qualified member functions to be more obvious
  • Example: class Cell { void setValue(Cell& self) { self.m_value = 42; } }
  • Enables writing recursive lambda expressions

If Const Eval

  • Syntax: if const eval a else b
  • Simplifies compile-time evaluation checks
  • More robust than isConstantEvaluated function

Multidimensional Subscript Operator

  • Allows proper multidimensional indexing using operator[]
  • Simplifies access to multidimensional data structures

Attributes on Lambda Expressions

  • Attributes can now be placed before or after parentheses
  • New placement modifies effects on the function call operator

New Integer Literal Surfaces

  • Introduces UZ (size_t) and Z (signed size type)
  • Improves type safety in integer operations

New Preprocessor Markers

  • Introduces lifdef, lifendef for improved preprocessor directives
  • New warning directive to emit compiler warnings

Unreachable Code

  • Use std::unreachable to mark unreachable code sections
  • Helps compiler optimize performance by omitting unnecessary checks

Compiler Optimizations

  • Introduces standard assumptions to help optimize code
  • Use [[assume]] syntax for performance hints

Example:

void divideBy32(int x) [[assume(x > 0)]];

Standard Library Features

String Formatting Improvements

  • New std::print and std::println simplify output operations
  • Much faster than previous std::cout and std::format

New Containers

  • Introduces flat_map, flat_set for optimized performance
  • Uses underlying sequence containers for better memory management

std::span and md_span

  • md_span supports multidimensional views and layout policies

Generators

  • Standard coroutine generator for simplifying value sequences
  • Example of a generator function iterating through values

Optional Monadic Operations

  • New monadic operations (transform, and_then, or_else)
  • Allows chaining operations without explicit checks for emptiness

Stack Traces

  • Use std::stacktrace to retrieve current stack trace easily
  • Can embed stack trace information in custom exceptions

Ranges

  • New features: zip, pairwise, chunk, join, and more
  • Enhancements for handling ranges and views

Expectation Type

  • Introduces std::expected to represent value or error states
  • Simplifies error handling in functions

Move-Only Functions

  • std::move_only_function for lambdas with captured variables

Conclusion

  • C++23 introduces many new features to enhance performance, usability, and expressiveness
  • Encourages developers to explore and adopt new language features

Questions

  • Open floor for questions
  • Discussed implications of infinite views and performance optimization strategies.