Search⌘ K

Awaitables and Awaiters

Explore the roles of awaitables and awaiters in C++20 coroutines. Understand how promise object functions like initial_suspend and final_suspend return awaitables that determine coroutine pausing behavior through the co_await operator. Learn about the Awaitable concept and built-in types like std::suspend_always and std::suspend_never, and how awaiters are obtained during coroutine execution.

The three functions of a promise object: yield_value, initial_suspend, and final_suspend return awaitables.

Awaitables

An Awaitable is something you can await on. The awaitable determines if the coroutine pauses or not.

Essentially, the compiler generates the three functions calls using the promise prom and the co_await operator.

Call Compiler generated call
yield value co_await prom.yield_value(value)
prom.initial_suspend() co_await prom.initial_suspend()
prom.final_suspend() co_await prom.final_suspend()

The co_await operator needs an awaitable as argument. Awaitables have to implement the concept Awaitable.

The concept Awaitable

The concept Awaitable requires three ...