IT lexicon › Programming › C++ coroutines

C++ coroutines

Programming På svenska → Updated: 2026-07-29

Functions that can pause partway through and resume later, introduced in C++20. The language shipped the machinery but forgot to include anything usable to run on it.

A function containing co_await, co_yield or co_return is a coroutine, and the compiler rewrites it into a state machine whose local variables move into a frame on the heap — unless the optimiser manages to elide the allocation. It is powerful and nearly free at runtime. The problem is that C++20 only specified the interfaces: a promise_type you have to write yourself, an awaiter protocol, a handle. No generator, no task, no scheduler. The result was that the feature was effectively unusable without an external library such as cppcoro or asio. C++23 finally added std::generator.

← Back to the lexicon