What are Coroutines in C++20, and what are their primary use cases?
Last updated
Coroutines are functions that can suspend and resume, preserving local state (usually in a heap frame). Any function using
co_await,co_yield, orco_returnis one.Main uses: async I/O written sequentially with
co_await, lazy generators viaco_yield, and callback-free state machines.C++20 provides only the language machinery, so you need a library or custom promise types (
std::generatorarrives in C++23).