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 <string>
#include "fibo4.hpp"
int main()
{
auto coro = fibonacci(20); // initialize the coroutine
// just some other stuff, which should not impact coro
std::string s{"impact"};
auto coro2 = fibonacci(7); // may impact the behavior of coro
// iterate over the values of the coroutine:
for (auto val : coro) {
std::cout << val << ' ';
}
std::cout << '\n';
}