James McNellis在他的演讲“C++协程简介”(https://youtu.be/ZTqHjjm86Bw?t=1898)中说:在以下情况下,协程会被破坏: final_suspend恢复, coroutine_handle ::destroy()被调用,以先发生的为准。在测试中,我看到了(VS 2015,VS 2017 RC),恢复在final_suspend上挂起的协程将导致错误: Unhandled exception at 0x010B9EDD in Awaits2017.exe: RangeChecks instrumentation code detected an out of range array access. occurred任何想法可能在这里发生什么?#include <experimental/resumable>using namespace std;using namespace std::experimental;struct Coro{ coroutine_handle<> m_coro; Coro(coroutine_handle<> coro) : m_coro(coro) {} struct promise_type { Coro get_return_object() { return Coro(coroutine_handle<promise_type>::from_promise(*this)); } auto initial_suspend() { return false; } auto final_suspend() { return true; } void return_void() {} };};Coro simple(){ co_return;}int main(){ Coro c = simple(); c.m_coro.resume(); // runtime error here} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 经验法则:如果final_suspend返回true,则应调用coroutine_handle<>::destroy()而不是resume()。 如果final_suspend返回false,则不应同时调用destroy(),协程将自行清理。 请注意,VS 2015中包含的协程不是视频中显示的James McNellis的内容(该提案进行了多次修订),其说明如下: final_suspend is resumed可能会令人困惑。这并不意味着resume()被调用。 (adsbygoogle = window.adsbygoogle || []).push({});