How to pass values between C++20 Coroutines?

Viewed 61

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:

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 expr is called this is transformed into co_await my_promise_type.yield_value(expr), which should return std::suspend_always.

  • Later the coroutine is resumed by calling resume() on the handle, which will call awaiter.await_resume() whose return value will be the type of the whole co_await epression.

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?

0 Answers
Related