#include #include using namespace std ; // 1. Define the return type that the compiler looks for struct SimpleCoroutine { struct promise_type { //Invoked when the coroutine is first invoked //by the caller. SimpleCoroutine get_return_object() { cout << "get_return_object()" << endl ; return {}; } //Called by the frame work. Before runnng the coroutine //the frame work checks whether to initially suspend or not. std::suspend_never initial_suspend() { cout << "initial_suspend()" << endl ; return {}; } std::suspend_never final_suspend() noexcept { cout << "suspend_never final_suspend()" << endl ; return {}; } void return_void() { cout << "return_void()" << endl ; } void unhandled_exception() { cout << "unhandled_exception()" << endl ; } }; }; // 2. The coroutine function itself SimpleCoroutine myFirstCoroutine() { std::cout << "Hello " << endl ; // This pauses the function right here! //Returns control to the main. Only 1 thread exists. co_await std::suspend_always{}; std::cout << "World!" << std::endl; } int main() { std::cout << "Inside Main."; // Starts execution, prints "Hello ", then suspends //constructs the promise_type object auto coro = myFirstCoroutine(); std::cout << "from Main... "; // In a minimal "get_return_object" setup like this, // the coroutine completes or handles execution based on the promise. return 0; }