std::coroutine_handle, std::noop_coroutine_handle
来自 cppreference.cn
定义于头文件 <coroutine> |
||
template< class Promise = void > struct coroutine_handle; |
(1) | (since C++20) |
template<> struct coroutine_handle<void>; |
(2) | (since C++20) |
template<> struct coroutine_handle<std::noop_coroutine_promise>; |
(3) | (since C++20) |
using noop_coroutine_handle = std::coroutine_handle<std::noop_coroutine_promise>; |
(4) | (since C++20) |
类模板 coroutine_handle
可以用于引用一个挂起或正在执行的协程。coroutine_handle
的每个特化都是一个 LiteralType。
1) 主模板,可以从
Promise
类型的 promise 对象创建。2) 特化 std::coroutine_handle<void> 擦除 promise 类型。它可以从其他特化转换而来。
3) 特化 std::coroutine_handle<std::noop_coroutine_promise> 指的是空操作协程。它不能从 promise 对象创建。
在典型的实现中,std::coroutine_handle
的每个特化都是 TriviallyCopyable。
如果程序为 std::coroutine_handle
添加特化,则行为是未定义的。
目录 |
[编辑] 数据成员
成员名称 | 定义 |
ptr (私有) |
指向协程状态的 void* 指针。 (仅用于说明的成员对象*) |
[编辑] 成员函数
构造一个 coroutine_handle 对象(公共成员函数) | |
赋值 coroutine_handle 对象(公共成员函数) | |
转换 | |
获取类型擦除的 coroutine_handle (公共成员函数) | |
观察器 | |
检查协程是否已完成 (公共成员函数) | |
检查句柄是否表示一个协程 (公共成员函数) | |
控制 | |
恢复协程的执行 (公共成员函数) | |
销毁一个协程 (公共成员函数) | |
Promise 访问 | |
访问协程的 promise (公共成员函数) | |
[静态] |
从协程的 promise 对象创建一个 coroutine_handle (公共静态成员函数) |
导出/导入 | |
导出底层地址,即支持协程的指针 (公共成员函数) | |
[静态] |
从指针导入一个协程 (公共静态成员函数) |
[编辑] 非成员函数
(C++20) |
比较两个 coroutine_handle 对象(函数) |
[编辑] 辅助类
对 std::coroutine_handle 的 hash 支持 (类模板特化) |
[编辑] 注释
coroutine_handle
可能会悬空,在这种情况下,必须谨慎使用 coroutine_handle
以避免未定义行为。
[编辑] 示例
运行此代码
#include <coroutine> #include <iostream> #include <optional> template<std::movable T> class Generator { public: struct promise_type { Generator<T> get_return_object() { return Generator{Handle::from_promise(*this)}; } static std::suspend_always initial_suspend() noexcept { return {}; } static std::suspend_always final_suspend() noexcept { return {}; } std::suspend_always yield_value(T value) noexcept { current_value = std::move(value); return {}; } // Disallow co_await in generator coroutines. void await_transform() = delete; [[noreturn]] static void unhandled_exception() { throw; } std::optional<T> current_value; }; using Handle = std::coroutine_handle<promise_type>; explicit Generator(const Handle coroutine) : m_coroutine{coroutine} {} Generator() = default; ~Generator() { if (m_coroutine) m_coroutine.destroy(); } Generator(const Generator&) = delete; Generator& operator=(const Generator&) = delete; Generator(Generator&& other) noexcept : m_coroutine{other.m_coroutine} { other.m_coroutine = {}; } Generator& operator=(Generator&& other) noexcept { if (this != &other) { if (m_coroutine) m_coroutine.destroy(); m_coroutine = other.m_coroutine; other.m_coroutine = {}; } return *this; } // Range-based for loop support. class Iter { public: void operator++() { m_coroutine.resume(); } const T& operator*() const { return *m_coroutine.promise().current_value; } bool operator==(std::default_sentinel_t) const { return !m_coroutine || m_coroutine.done(); } explicit Iter(const Handle coroutine) : m_coroutine{coroutine} {} private: Handle m_coroutine; }; Iter begin() { if (m_coroutine) m_coroutine.resume(); return Iter{m_coroutine}; } std::default_sentinel_t end() { return {}; } private: Handle m_coroutine; }; template<std::integral T> Generator<T> range(T first, const T last) { while (first < last) co_yield first++; } int main() { for (const char i : range(65, 91)) std::cout << i << ' '; std::cout << '\n'; }
输出
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 3460 | C++20 | coroutine_handle 的公共基类可能使其处于不期望的状态 |
移除继承 |
[编辑] 参见
(C++23) |
表示同步协程生成器的 view (类模板) |