#include #include #include #include #define TRACK() printf("%s\n", __PRETTY_FUNCTION__) struct awaitable // std::suspend_always carbon copy { awaitable() { TRACK(); } ~awaitable() { TRACK(); } constexpr bool await_ready() const noexcept { TRACK(); return false; } constexpr void await_suspend(std::coroutine_handle<>) const noexcept { TRACK(); } constexpr void await_resume() const noexcept { TRACK(); } }; struct [[nodiscard]] fiber { struct promise_type // coroutine controller { promise_type() { TRACK(); } ~promise_type() { TRACK(); } fiber get_return_object() { TRACK(); return handle_type::from_promise(*this); } awaitable initial_suspend() noexcept { TRACK(); return {}; } awaitable final_suspend() noexcept { TRACK(); return {}; } void return_void() { TRACK(); } void unhandled_exception() { TRACK(); std::terminate();} }; using handle_type = std::coroutine_handle; handle_type handle = nullptr; fiber(handle_type h): handle(h) { TRACK(); } ~fiber() { TRACK(); if (handle) handle.destroy();} }; fiber make_fiber() { TRACK(); for (auto i: {0, 1, 2, 3, 4}) { printf("%i ", i); co_await awaitable{}; } } int main() { TRACK(); auto t = make_fiber(); while (!t.handle.done()) t.handle.resume(); }