Why C++ Coroutines Allocate on the Heap

A major complain of C++ coroutines is that it dynamically allocate by default and relies on compiler heap-elision.

One reason for that design decision I can think of is that not having layout determined in frontend gives the optimizer opportunity to optimize out some variables.

However, this insightful r/cpp comment highlights that storing coroutine states on the stack could limit them to header files (or module interface files) and can easily break ABI. Both are not a big problem for Rust since Rust’s TUs are much larger (a crate) and have an unstable ABI by default.

Also see this table from Coroutines: Use-cases and Trade-offs: type-erased coroutines vs embedded coroutines.png