Generating a sequence of values with the std::generator type
The C++20 standard includes two major updates to the standard library: the ranges library and coroutines. However, with regard to the latter, the support is minimal. The C++20 standard only defines a framework for building coroutines. Because of this, libraries such as libcoro, which we have previously seen, were created to provide actual coroutines, such as task
and generator
, which we also saw in the previous two recipes. The C++23 standard introduces the first standard coroutine, called std::generator
. This brings together ranges and coroutines because std::generator
is a view that represents a synchronous coroutine generator. This is a standard implementation for what we explicitly built in the previous recipe, Creating a coroutine generator type for sequences of values. Let’s see how it works.
At the time of writing, only GCC 14 supports this standard coroutine.
How to do it…
To...