TheDocumentation 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_return statement is a C++20 control flow keyword used exclusively within a coroutine to terminate its execution and optionally produce a final result. Its presence in a function body is one of the syntactic triggers that instructs the compiler to transform the function into a state machine rather than a standard subroutine.
Syntax
Underlying Mechanics
Unlike a standardreturn statement, which places a value in a register or on the stack and pops the stack frame, co_return interacts directly with the coroutine’s associated promise_type. The compiler translates the co_return statement into a specific sequence of method calls on the promise object based solely on the type of the operand.
1. Non-Void Return
Whenco_return expression; or co_return { braced-init-list }; is evaluated, and the operand is not of type void, the compiler translates it to:
2. Void Return
Whenco_return; is evaluated without an operand, the compiler translates it to:
co_return expression; is evaluated and the expression has a type of void, the C++ standard mandates that the void expression must be evaluated before promise.return_void() is called. The compiler conceptually translates this to:
Execution Flow
When aco_return statement is encountered, the coroutine executes the following sequence:
- Promise Mutation: The compiler invokes either
promise.return_value(<expr>)orpromise.return_void(), evaluating any void expressions beforehand if applicable. - Local Destruction: All variables with automatic storage duration in the coroutine’s local scope are destroyed in reverse order of their construction.
- Final Suspension: Control is transferred to the final suspension point. The compiler implicitly executes
co_await promise.final_suspend();. This allows the coroutine to either suspend one last time (keeping the state alive for the caller to read the result) or destroy itself immediately.
Compiler Constraints and Rules
- Mutual Exclusion with
return: A function cannot contain bothreturnandco_returnstatements. Using a standardreturninside a coroutine results in a compilation error. - Promise Type Requirements: Under C++20 semantics, the definition of a coroutine is ill-formed if its promise type declares both
return_voidandreturn_value. This rule applies to the mere definition of the coroutine, regardless of whether anyco_returnstatements are actually present in the coroutine body. - Implicit
co_return: If execution reaches the end of a coroutine body without encountering aco_returnstatement, the compiler evaluates the promise type. Ifpromise.return_void()is a valid expression (e.g., it is declared, accessible, and satisfies overload resolution), the compiler implicitly injectsco_return;at the end of the body. Ifpromise.return_void()is not a valid expression, flowing off the end of the coroutine results in undefined behavior. - Main Function: The
mainfunction cannot be a coroutine; therefore,co_returncannot be used insidemain.
Master C++ with Deep Grasping Methodology!Learn More





