Boost.Cobalt generators
As discussed in Chapter 8, generator coroutines are specialized coroutines designed to yield values incrementally. After each value is yielded, the coroutine suspends itself until the caller requests the next value. In Boost.Cobalt, generators work in the same way. They are the only coroutine type that can yield values. This makes generators essential when you need a coroutine to produce multiple values over time.
One key characteristic of Boost.Cobalt generators is that they are eager by default, meaning they start execution immediately after being called. Additionally, these generators are asynchronous, allowing them to use co_await
, an important difference from std::generator
introduced in C++23, which is lazy and doesn’t support co_await
.
Looking at a basic example
Let’s begin with the simplest Boost.Cobalt program. This example is not that of a generator, but we will explain some important details with its help:
#include <iostream...