#include <coroutine>
#include <iostream>
struct Generator
{
struct promise_type
{
long long current_value;
Generator get_return_object()
{
return Generator{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
std::suspend_always yield_value(long long value)
{
current_value = value;
return {};
}
void return_void() {}
void unhandled_exception() {}
};
std::coroutine_handle<promise_type> handle;
Generator(std::coroutine_handle<promise_type> h) : handle(h) {}
~Generator() { if (handle) handle.destroy(); }
long long next()
{
handle.resume();
return handle.promise().current_value;
}
};
Generator fibonacci()
{
long long a = 0, b = 1;
while (true) {
co_yield a;
auto tmp = a + b;
a = b;
b = tmp;
}
}
int main()
{
auto gen = fibonacci();
long long n = gen.next();
while (n < 100) {
std::cout << n << " ";
n = gen.next();
}
return 0;
}