How much memory must be reserved for a C++20 coroutine frame?

This was debated at length during the standardization of C++20 coroutines. The layout and size of the coroutine frame cannot be determined until after the optimizer finishes its job, and making that information available to the frontend would require fundamental rearchitecturing of all existing compilers. Implementers reported that not even a (useful) upper bound is feasible.

See parts 2 and 4 of P1365R0 for a discussion on ways to use coroutines in environments where no dynamic memory allocation is allowed.


What tools does the C++ language provide to query the size of a coroutine frame?

None. What you want is impossible by design.

co_await coroutines in C++ are designed in such a way that being a coroutine is an implementation detail of the function. From just a function declaration, it is impossible to know if a function is a coroutine or if it just happens to have a signature that could use the various coroutine machinery. The feature is meant to work in such a way that it's effectively none of your business if a function is or is not a coroutine.

Being able to determine the size of a coroutine frame would first require being able to identify a coroutine. And since the system is designed such that this is impossible... well, there you are.


As Nicol Bolas mentioned it is not possible to get it as a constexpr value. But this is not possible for "normal functions", too. There is just one rule "don't store big objects on the stack to avoid stackoverflows".

As a role of thumb the maximum of the required heap storage is the size of your local variables that must be available after the first continuation and eventually some small "management-fields" to store the contunation-point (usually some kind of int).

But our compilers are nowadays really smart and optimize the heap allocations completely away - so you should not worry to much about it.