Vro C++ coroutines are kind of bullshit like what do you mean you gotta do hacks like this to keep track of what coroutine is currently running
#include <coroutine>
#include <exception>
#include <cstdio>
struct task;
struct pretask;
struct promise;
struct task {
using promise_type = promise;
task() = default;
task(pretask&& o);
};
struct pretask {
task x;
pretask() = default;
};
task::task(pretask&& o) : task(o.x) { printf("]] Leave coro (initial)\n"); }
struct promise {
pretask get_return_object() { return {}; }
std::suspend_never initial_suspend() noexcept {
printf("Enter coro (initial) [[\n");
return {};
}
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() { std::terminate(); }
};
struct awaiter {
std::coroutine_handle<> handle;
bool await_ready() { return false; }
void await_suspend(std::coroutine_handle<> h) { handle=h; }
void await_resume() { }
void resume() {
printf("Enter coro (resume) [[\n");
if (handle)
handle.resume();
handle = {};
printf("]] Leave coro (resume)\n");
}
};
task nya(awaiter& awa) {
printf(" Meow\n");
co_await awa;
printf(" Meow2\n");
co_return;
}
int main() {
awaiter awa;
printf("== Begin Nya ==\n");
auto x = nya(awa);
printf("== Resume Nya ==\n");
awa.resume();
printf("== End Nya ==\n");
}
#cpp #cplusplus