Using std::generator<> in Practice

Speaker: Nicolai Josuttis

Audience level: 7

C++20 introduced coroutines, which are functions that can be subspended and later resumed (in the same or a different context). As a consequence, coroutines allow programs to switch back and forth between different paths of execution. However, C++20 only provided low-level support, which is highly flexible but requires a lot of boilerplate code to implement even simple coroutine use cases.

One key element of this boilerplate code are coroutine interfaces, which

  • serve as standard customization point for the compiler
    to configure general aspects of the coroutine and
  • define the API to deal with the coroutine.

With std::generator<>, C++23 introduces a  first standard coroutine interface for the use cases, where a coroutine generates a sequence of values, which the user(s) of the coroutine can process using a range interface.

This talks introduces the std::generator<> class template, explains how it can be used in practice, provides practical insights about special attributes, and discusses various pitfalls you shouldhave in mind when using coroutines and std::generator<>.