#include #include #include #include #include namespace zt { namespace demo{ template struct Promise; template struct coroutine: std::coroutine_handle>{ using promise_type = Promise; }; struct RAII{ std::string name; RAII(const std::string &s):name(s){ std::cout<<__FUNCTION__<<" "< struct Promise{ Promise():raii("in promise"){}; RAII raii; auto initial_suspend(){ std::cout<<__FUNCTION__<<'\n'; return std::suspend_always{}; // return std::suspend_never{}; } auto final_suspend()noexcept{//最后要不要把句柄执行权限归还,如果不归还,那么协程被销毁,继续检查.done()则Segmentation fault std::cout<<__FUNCTION__<<'\n'; return std::suspend_always{}; } void unhandled_exception(){ std::cout<<__FUNCTION__<<'\n'; throw std::runtime_error("unhandled exeception\n"); } void return_value(const std::decay_t &t){ std::cout<<__FUNCTION__<<' '< get_return_object(){ std::cout<<__FUNCTION__<<'\n'; return {coroutine::from_promise(*this)}; } auto yield_value(const std::decay_t &t){ std::cout<<__FUNCTION__<<' '< h; }; } }