title: How much memory must be reserved for a C++20 coroutine frame?
body: <p>First of all I want to predict the memory usage of my code, just as any responsible programmer should. This would apply even if I was <em>not</em> deciding to allocate my coroutine frames using <em>placement new</em>, as I am (see below pseudocode). <em>Even supposing I change my mind about placement-newing all my coroutines, and thus I let the complier allocate all my coroutines on the heap, I'd still want the C++ language to tell me how much heap I'm going to eat up by that.</em></p>
  <p>But, IRL, I'm targeting a high-reliability and embedded environment. There might not even <em>be</em> a heap, so...</p>
  <pre><code>struct coroutine_return_type
  {
    struct promise_type
    {
      void *operator new(std::size_t sz, char *buf, std::size_t szbuf)
      {
        if (sz &gt; szbuf)
          throw std::bad_alloc{};
        return buf;
      }
      void operator delete(void *)
      {
      }
      // ...
    };
    // ...
  };
  
  coroutine_return_type my_coroutine(char *, std::size_t)
  {
    // The arguments, char * and std::size_t,
    // have been fowrarded to promise_type::operator new
    // but here in the coroutine body they aren't used again...
    for ( ; ; )
      co_yield /* something */;
  }
  
  struct coroutine_instance_type
  {
    char my_coroutine_frame[ /* WHAT? */ ];
    coroutine_return_type my_coroutine_instance;
    coroutine_instance_type()
      : my_coroutine_instance{my_coroutine(my_coroutine_frame, sizeof(my_coroutine_frame))}
    {
      // ...
    }
    // ...
  };
  </code></pre>
  <p><strong>WHAT I WANT</strong></p>
  <p>I want a complie-time expression to return an upper bound on my coroutine size, to replace <code>/* WHAT? */</code>.</p>
  <p><strong>STUPID SOLUTION</strong></p>
  <p>There's an obviously stupid way to (not quite) do what I want:</p>
  <ol>
  <li><p>Subclass std::bad_alloc. Then <code>throw std::bad_alloc{}</code> in my <code>operator new</code> becomes <code>throw std::my_bad_alloc{sz}</code>. The catch block can call <code>my_bad_alloc_instance.get_parameter()</code> to learn what <code>sz</code> was inside <code>operator new</code>.</p>
  </li>
  <li><p>Call <code>my_coroutine(nullptr, 0)</code> and catch the exception.</p>
  </li>
  </ol>
  <p>What's stupid about this (nonexhaustive list):</p>
  <p>It's not a compile-time expression, because it has to &quot;return&quot; its value using a <code>throw</code> and <code>throw</code> can never be used in a complie-time expression. But the replacement for <code>/* WHAT? */</code> in my pseudocode needs to be a compile-time expression.</p>
  <p>It's a sample, not an upper bound. Suppose the actual, allocated size of the coroutine frame depends on conditions at run-time. (Now, <em>I don't anticipate that different coroutine sizes for different run-time conditions, will ever actually occur in my IRL applcation</em>, but according to the C++ standard it seems to be possible.) In that case, it's insufficient to just learn what size is actually passed to <code>operator new</code>. The required expression would have to return, instead, an <em>upper bound</em> on what <em>could be</em> passed to <code>operator new</code>.</p>
  <p>So, in summary:</p>
  <p><strong>SUMMARY OF THE QUESTION</strong></p>
  <p>What tools does the C++ language provide to query the size of a coroutine frame? The ideal tool should be a compile-time expression for allocating non-heap memory to the coroutine, or alternatively, the same tool would serve as well for bounding the amount of heap.</p>