In C++20 the new Coroutines TS is being introduced. This is the foundation to build coroutine-logic from.
There are some examples online of how one might build e.g. generators, tasks and other abstractions on top, such as https://github.com/Quuxplusone/coro/ and https://github.com/lewissbaker/cppcoro
However, so far I've been unable to find any example of how passing a value to a partially-executed coroutine. This is rather common, as examples of coroutine usage in other languages show. A few examples:
- Python3 implementation of Conway's Game of Life using coroutines: code, blogpost explanation
- Ruby's yield implementation.
- passing multiple values to a Boost.Coroutine
I have so far not been able to wrap my head around how this would work with the Coroutines TS, however. This is how far I got:
When
co_yield expris called this is transformed intoco_await my_promise_type.yield_value(expr), which should returnstd::suspend_always.Later the coroutine is resumed by calling
resume()on the handle, which will callawaiter.await_resume()whose return value will be the type of the wholeco_awaitepression.
Is this correct? Is there something missing? How is the compiler able to figure out the types here? Are there any example implementations available of coroutine-types that currently implement this?