co_await expr: Evaluates an awaitable expression, potentially suspending the coroutine while waiting for a computation.co_yield expr: Suspends execution and yields a value to the caller. This is syntactic sugar forco_await promise.yield_value(expr).co_return expr: Evaluates the return expression, invokespromise.return_value(expr)(orpromise.return_void()), destroys local variables in reverse order of creation, and subsequently transitions execution to the implicit final suspend point.
The Mechanics of Suspension
When the compiler encounters a suspend point viaco_await, it evaluates an Awaitable expression. The compiler converts this Awaitable into an Awaiter object (e.g., via operator co_await() or the promise type’s await_transform()). The Awaiter must implement three specific methods that dictate the exact behavior of the suspension:
await_ready(): Returns abool. Iftrue, the coroutine does not suspend, avoiding the overhead of context switching.await_suspend(std::coroutine_handle<PromiseType>): Called immediately after the coroutine is considered suspended and its state is saved, but before control is returned to the caller. It receives a strongly-typed handle to the suspended coroutine (though the awaiter may choose to accept a type-erasedstd::coroutine_handle<void>via implicit conversion). Because the coroutine is already suspended at this stage, another thread can resume (and potentially destroy) the coroutine whileawait_suspendis still executing.await_resume(): Called to obtain the result of theco_awaitexpression. If the coroutine suspended, this is called immediately after resumption. Ifawait_ready()returnedtrue, it is called immediately without any suspension occurring.
Compiler Transformation
To visualize the mechanics of a suspend point, consider the following conceptual transformation performed by the C++ compiler when it encounters aco_await expression:
Implicit Suspend Points
In addition to explicit keywords within the coroutine body, the C++ coroutine specification mandates two implicit suspend points managed by the coroutine’s Promise type:- Initial Suspend Point: Occurs immediately after the coroutine frame is allocated, arguments are copied, and the promise object is constructed, but before the user-authored coroutine body begins executing. It is executed as
co_await promise.initial_suspend(). - Final Suspend Point: Occurs after the coroutine body has finished executing (via
co_returnor falling off the end) and local variables have been destroyed. It is executed asco_await promise.final_suspend(). Suspending at the final suspend point keeps the coroutine frame alive so the caller can extract the final result or observe the completion state.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More





