What do you think about this?#include <alloca.h>#include <setjmp.h>#include <cassert>#include <functional>template <::std::size_t N = 4096>class coroutine{  jmp_buf env_in_;  jmp_buf env_out_;  bool running_{};  char stack_[N];public:  coroutine() = default;  auto running() const noexcept  {    return running_;  }  template <typename F, typename ...A>  void run(F&& f, A&& ...a)  {    if (setjmp(env_in_))    {      return;    }    // else do nothing    auto top(reinterpret_cast<char*>(&top));    alloca(top - (stack_ + N));    running_ = true;    [this, f = ::std::forward<F>(f)](A&& ...a) __attribute__ ((noinline))    {      f(::std::ref(*this), ::std::forward<A>(a)...);      running_ = false;      yield();    }(::std::forward<A>(a)...);  }  void yield() noexcept  {    if (setjmp(env_out_))    {      return;    }    else    {      longjmp(env_in_, 1);    }  }  void resume()  {    assert(running_);    if (setjmp(env_in_))    {      return;    }    else    {      longjmp(env_out_, 1);    }  }};Usage:#include <iostream>#include coroutine.hppint main(){  coroutine<> c;  c.run([](decltype(c)& c)    {      for (int i{}; i != 3; ++i)      {        ::std::cout << i << ::std::endl;        c.yield();      }    }  );  while (c.running())  {    c.resume();  }  return 0;}EDITChangeauto top(reinterpret_cast<char*>(&top));tochar* top;top = reinterpret_cast<char*>(&top);Also check out an updated version of the code.
Small coroutine class
c++;c++14