#include #include #include #include struct ReturnObject { struct promise_type { ReturnObject get_return_object() { return {}; } std::suspend_never initial_suspend() { return {}; } std::suspend_never final_suspend() noexcept { return {}; } void unhandled_exception() {} }; }; struct Awaiter { std::coroutine_handle<> *hp_; constexpr bool await_ready() const noexcept { std::cout << "Awaiter::await_ready()" << std::endl; return false; } void await_suspend(std::coroutine_handle<> h) { std::cout << "Awaiter::await_suspend()" << std::endl; *hp_ = h; } constexpr void await_resume() const noexcept { std::cout << "Awaiter::await_resume()" << std::endl; } }; ReturnObject counter(std::coroutine_handle<> *continuation_out) { Awaiter a{continuation_out}; for (unsigned i = 0;; ++i) { co_await a; std::cout << "counter: " << i << std::endl; } } int main() { std::coroutine_handle<> h; counter(&h); for (int i = 0; i < 3; ++i) { std::cout << "In main function\n"; h(); } h.destroy(); }