Shows how to (ab)use begin()
to advance the iterator.
#include <iostream>
#include <utility>
namespace {
{
co_return n + 1;
}
{
int v = 0;
while (v < n) {
co_yield v;
v = co_await get_next_value(v);
}
}
{
int v = 0;
while (v < n) {
co_yield v;
++v;
}
}
{
auto gen = async_first_n(5);
auto it = co_await gen.begin();
auto end = gen.end();
do {
std::cout << *it << " ";
} while (co_await gen.begin() != end);
std::cout << "\n";
}
void iterate_over_sync()
{
auto gen = sync_first_n(5);
auto it = gen.begin();
auto end = gen.end();
do {
std::cout << *it << " ";
} while (gen.begin() != end);
std::cout << "\n";
}
void iterate_over_adapted_async()
{
auto async = async_first_n(5);
auto it = gen.begin();
auto end = gen.end();
do {
std::cout << *it << " ";
} while (gen.begin() != end);
std::cout << "\n";
}
}
int main()
{
std::cout << "Iterating over asynchronous generator:\n";
iterate_over_async();
std::cout << "Iterating over synchronous generator:\n";
iterate_over_sync();
std::cout << "Iterating over adapted asynchronous generator:\n";
iterate_over_adapted_async();
return 0;
}
An asynchronous generator that produces values of type Result.
An synchronous generator that produces values of type Result.
sync_generator_adapter(async_generator< Result >) -> sync_generator_adapter< Result >
Deduction guide for sync_generator_adapter.
Adapter for converting asynchronous generators to synchronous generators.