wwa-coro 0.0.1
Yet Another C++20 Coroutine Library
generator_iterator.cpp

Example of using generator iterators with range for and mnaually.

#include <cstddef>
#include <iostream>
#include "generator.h"
namespace {
wwa::coro::generator<int> first_n(std::size_t n)
{
int v = 0;
while (static_cast<std::size_t>(v) < n) {
co_yield v;
++v;
}
}
} // namespace
int main()
{
std::cout << "The first 5 numbers are:\n";
for (auto n : first_n(5)) {
std::cout << n << ' ';
}
std::cout << '\n';
auto gen = first_n(5);
for (auto it = gen.begin(); it != gen.end(); ++it) { // NOSONAR
std::cout << *it << ' ';
}
std::cout << '\n';
// The expected output is:
// The first 5 numbers are:
// 0 1 2 3 4
// 0 1 2 3 4
return 0;
}
An synchronous generator that produces values of type Result.
Definition generator.h:43
Synchronous generator.