真正干事的是__builtin_coro_done, __builtin_coro_resume和__builtin_coro_destroy, coroutine_handle只是在void*外面包了一层。它没有析构函数,得去调destroy(). 它重载了(), h()等于h.resume()等于__builtin_coro_resume(ptr);
示意:
template<> struct coroutine_handle<void> { void* m_ptr; bool done() { return __builtin_coro_done(m_ptr); } void resume() { __builtin_coro_resume(m_ptr); } void operator()() { resume(); } void destroy() { __builtin_coro_destroy(m_ptr); } operator bool() { return bool(m_ptr); } void* address() { return m_ptr; } static coroutine_handle from_address(void*); coroutine_handle(); // https://en.cppreference.com/w/cpp/types/nullptr_t // coroutine_handle h(0); coroutine_handle h(NULL); h = 0; h = NULL; // oneptr_t, twoptr_t ... _10000ptr_t? coroutine_handle(std::nullptr_t); coroutine_handle& operator=(std::nullptr_t); };
D:\gcc\lib\gcc\mingw32\10.3.0\include\c++\coroutine: tab size不为8时更乱:
template <> struct coroutine_handle<void> { public: // 17.12.3.1, construct/reset constexpr coroutine_handle() noexcept : _M_fr_ptr(0) {} constexpr coroutine_handle(std::nullptr_t __h) noexcept : _M_fr_ptr(__h) {} coroutine_handle& operator=(std::nullptr_t) noexcept { _M_fr_ptr = nullptr; return *this; } public: // 17.12.3.2, export/import constexpr void* address() const noexcept { return _M_fr_ptr; } constexpr static coroutine_handle from_address(void* __a) noexcept { coroutine_handle __self; __self._M_fr_ptr = __a; return __self; } public: // 17.12.3.3, observers constexpr explicit operator bool() const noexcept { return bool(_M_fr_ptr); } bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); } // 17.12.3.4, resumption void operator()() const { resume(); } void resume() const { __builtin_coro_resume(_M_fr_ptr); } void destroy() const { __builtin_coro_destroy(_M_fr_ptr); } protected: void* _M_fr_ptr; }; // 17.12.3.6 Comparison operators /// [coroutine.handle.compare] constexpr bool operator==(coroutine_handle<> __a, coroutine_handle<> __b) noexcept { return __a.address() == __b.address(); }View Code
_M_fr_ptr咋就比m_ptr高档了?我倒是也觉得reinterpret_cast没鸟用。VC的coroutine也未必好看到哪里去,STL好像是SGI的相对最好看。封装是OOP的头一条,protected的成员变量用_打头有必要吗?struct默认public, class默认private,struct后紧跟public没必要。