#include #include #include template struct coro { struct promise_type { coro get_return_object() { return handle_type::from_promise(*this); } std::suspend_always initial_suspend() noexcept { return {}; } std::suspend_always final_suspend() noexcept { return {}; } void unhandled_exception() {} T value = std::numeric_limits::max(); std::suspend_always yield_value(T t) { value = t; return {}; } }; using handle_type = std::coroutine_handle; handle_type handle; coro(handle_type h): handle(h) {} ~coro() { if (handle) handle.destroy(); } }; template coro f(T n) { for (T i = 0; i < n; ++i) co_yield i; // co_await promise.yield_value(i); } int main() { coro c1 = f(10zu); while (!c1.handle.done()) // imprime valor por defecto { std::printf(" -> %zu", c1.handle.promise().value); c1.handle.resume(); } std::printf("\n"); coro c2 = f(10zu); while (!c2.handle.done()) // repite Ășltimo valor { c2.handle.resume(); std::printf(" -> %zu", c2.handle.promise().value); } std::printf("\n"); coro c3 = f(10zu); while (!c3.handle.done()) // verifica y no repite { c3.handle.resume(); if (!c3.handle.done()) std::printf(" -> %zu", c3.handle.promise().value); } std::printf("\n"); }