The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
#include "fibo1.hpp"
int main()
{
// initialize the coroutine:
auto coro = fibonacci();
// start the coroutine:
auto pos = coro.begin();
// while we are not at the end of the coroutine, print the yielded value and resume the coroutine:
while (pos != coro.end()) { // while suspended with a yielded value
std::cout << *pos << ' '; // print the value from co_yield
++pos; // RESUME the coroutine
}
std::cout << '\n';
}