#include #include #include struct CT { struct promise_type; using handle_type = std::coroutine_handle; struct promise_type { int m_Value; CT get_return_object() { return CT{ handle_type::from_promise(*this) }; } std::suspend_never initial_suspend() { return {}; } std::suspend_always final_suspend() noexcept { return {}; } std::suspend_always yield_value(int x) { m_Value = x; return {}; } void return_void() {}; void unhandled_exception() {}; }; handle_type m_Handle; CT(handle_type h) : m_Handle(h) {} ~CT() { m_Handle.destroy(); } int val() { return m_Handle.promise().m_Value; } void advance() { if(!m_Handle.done()) m_Handle.resume(); } bool done() { return m_Handle.done(); } }; CT gen_steps(int start, int end, int stride = 1) { for (int i = start; i < end; i += stride) { co_yield i; } } int main (int argc, char *argv[]) { for(auto gen = gen_steps(0, 10, 2); !gen.done(); gen.advance()) std::cout << gen.val() << std::endl; return 0; }