CKTIS / C++
co_yield — yielding a value from a coroutine
A co_yield expression passes its operand to the coroutine promise object's yield_value and awaits the result. In a generator, this exposes successive values without ending the function; whether execution actually suspends depends on the awaiting mechanism.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
co_yield assignment-expression
co_yield braced-init-listco_yield is a keyword forming an expression that yields a value from a coroutine. It may appear only in a function's suspension context. Its operand can be an assignment-expression or a braced initializer list. It is not an ordinary return.
For operand e and promise object p, co_yield e is equivalent to co_await p.yield_value(e). The promise type must therefore provide a suitable yield_value whose result supports the awaiting protocol. The keyword alone does not guarantee suspension: the resulting awaitable determines that behavior.
In the example, std::generator<int> exposes values one at a time. The loop in main resumes the coroutine, which supplies its next value and suspends at each co_yield. When resumed, execution continues after that expression. By contrast, co_return means completion of the coroutine, not delivery of another element.
The example requires a compiler and standard library supporting C++23 and <generator>. The -std=c++23 flag alone does not establish library availability or production runtime readiness. The source is an evolving working draft, not the frozen C++23 text.
Examples
#include <generator>
#include <iostream>
std::generator<int> numbers() {
co_yield 10;
co_yield 20;
}
int main() {
for (int value : numbers()) {
std::cout << value << '\n';
}
}
Result
10 20
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.