A coroutine in C++ is a function that can suspend execution to be resumed later, maintaining its local state across suspensions. A function becomes a coroutine implicitly if its body contains at least one of three specific keywords:Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
co_await, co_yield, or co_return. Unlike standard functions, coroutines do not use the standard return statement and rely on a compiler-generated state machine to manage execution flow.
Coroutine Keywords
co_await <expr>: Evaluates an awaiter object. Suspends the coroutine’s execution only if the awaiter’sawait_ready()method returnsfalse. If it returnstrue, the coroutine continues execution synchronously without suspending.co_yield <expr>: Syntactic sugar forco_await promise.yield_value(<expr>). Suspends the coroutine and returns a value to the caller, provided the resulting awaiter’sawait_ready()method returnsfalse.co_return <expr>: Terminates the coroutine and provides a final return value (orvoid).
Architectural Components
C++ coroutines are stackless and rely on a tripartite architecture consisting of the Coroutine State, the Promise Object, and the Coroutine Handle.1. Coroutine State
A compiler-generated, typically heap-allocated frame that persists across suspensions. It contains:- The promise object.
- Copied parameters passed to the coroutine.
- Local variables with a lifetime spanning a suspension point.
- The current execution state (instruction pointer).
2. The Promise Type
The promise object is manipulated from inside the coroutine. It dictates the coroutine’s behavior, such as initialization, termination, and exception handling. The return type of a coroutine must define a nestedpromise_type (or specialize std::coroutine_traits).
3. The Coroutine Handle
Thestd::coroutine_handle<promise_type> is a non-owning pointer manipulated from outside the coroutine. The caller uses it to interact with the suspended coroutine state.
The Awaiter Interface
Whenco_await <expr> is evaluated, <expr> must resolve to an Awaitable object. The compiler interacts with this object via the Awaiter interface to determine exactly how the suspension occurs.
std::suspend_always (always suspends) and std::suspend_never (never suspends).
Compiler Execution Flow
When a coroutine is invoked, the compiler injects boilerplate to manage the state machine:- Allocates the coroutine state using
operator new. - Copies function parameters into the coroutine state.
- Constructs the
promise_typeobject. - Calls
promise.get_return_object(). The result is kept as a local variable to be returned to the caller. - Begins an implicit
try-catchblock. - Executes
co_await promise.initial_suspend()inside thetry-catchblock. - Executes the actual body of the coroutine.
- If an exception escapes the body or the resume phase of
initial_suspend, it is caught and routed topromise.unhandled_exception(). - Upon reaching
co_returnor the end of the body, callspromise.return_void()orpromise.return_value(). - Destroys local variables in reverse order of creation.
- Exits the
try-catchblock. - Executes
co_await promise.final_suspend(). - State destruction depends on the behavior of
final_suspend:- If
final_suspendsuspends the coroutine (e.g., returnsstd::suspend_always), the caller or resumer is responsible for calling.destroy()on the handle to deallocate the state. - If
final_suspenddoes not suspend (e.g., returnsstd::suspend_never), the coroutine state is destroyed automatically upon completion. Manually calling.destroy()in this scenario results in undefined behavior (a double-free).
- If
Restrictions
A function cannot be a coroutine if it is:- A
constexprfunction. - A constructor or destructor.
- The
mainfunction. - A function using standard
returnstatements (a coroutine must exclusively useco_returnif it returns). - A function with variadic arguments (varargs), such as
void foo(int, ...). - A function with a placeholder return type (
autoordecltype(auto)).
Master C++ with Deep Grasping Methodology!Learn More





