Deciphering C++ Coroutines - A Diagrammatic Coroutine Cheat Sheet - Andreas Weis - CppCon 2022

Discover the intricacies of C++ coroutines with this comprehensive cheat sheet, covering topics like `await`, suspend points, promises, and customization options for advanced developers.

Key takeaways
  • C++ coroutines consist of the await keyword and the return statement.
  • Coroutines have three points for suspension: await_suspend, initial_suspend, and final_suspend.
  • When a coroutine is created, the return type becomes the coroutine’s result type.
  • When await_suspend is called, control flow returns to the caller, allowing them to store a handle to the coroutine.
  • When the promise is passed to getReturnObject, the await_suspend function is called.
  • Coroutines do not directly access their suspend points, they rely on their promise for suspension and cancellation.
  • Promises hold on to the result type, suspension points and destruction points of coroutines.
  • Initial suspend puts the coroutine to sleep.
  • Final suspend gets called when a coroutine returns and checks if there’s any unhandled exception.
  • Cancellation points determine when and if a coroutine’s execution will resume.
  • Co_yield acts like syntactic sugar, allows users to define what’s stored in the promise.
  • Coroutine Traits can be customized.
  • Initial Suspend and Final Suspend permit controlling what happens when a coroutine enters and leaves the suspension points.