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 <generator>
#include <ranges>
#include "fibo5.hpp"
std::generator<long> testFibonacci()
{
// yield the first 10 Fibonacci numbers:
co_yield std::ranges::elements_of(fibonacci(10));
co_yield 9999; // yield 9999 as separator
// yield the last two of the first 10 Fibonacci numbers again:
co_yield std::ranges::elements_of(fibonacci(10) | std::views::drop(8));
co_yield 9999; // yield 9999 as separator
// yield the first Fibonacci number:
co_yield std::ranges::elements_of(fibonacci(1));
}