std::coroutine_handle, std::noop_coroutine_handle
From cppreference.com
| ヘッダ <coroutine> で定義 |
||
| template< class Promise = void > struct coroutine_handle; |
(1) | (C++20以降) |
| template<> struct coroutine_handle<void>; |
(2) | (C++20以降) |
| template<> struct coroutine_handle<std::noop_coroutine_promise>; |
(3) | (C++20以降) |
| using noop_coroutine_handle = std::coroutine_handle<std::noop_coroutine_promise>; |
(4) | (C++20以降) |
クラス テンプレート coroutine_handle は、中断された、または実行中のコルーチンを参照するために使用できます。coroutine_handle のすべての特殊化は LiteralType です。
1) プライマリ テンプレート。型
Promise の promise オブジェクトから作成できます。2) 特殊化 std::coroutine_handle<void> は promise 型を消去します。他の特殊化から変換可能です。
3) 特殊化 std::coroutine_handle<std::noop_coroutine_promise> は、no-op コルーチンを参照します。promise オブジェクトから作成することはできません。
一般的な実装では、std::coroutine_handle のすべての特殊化は TriviallyCopyable です。
プログラムが std::coroutine_handle の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] データメンバー
| メンバ名 | 定義 |
ptr (private) |
コルーチン状態への void* ポインタ。 (説明用のメンバオブジェクト*) |
[編集] メンバ関数
coroutine_handle オブジェクトを構築する(public メンバ関数) | |
coroutine_handle オブジェクトを代入する(public メンバ関数) | |
変換 | |
型消去された coroutine_handle を取得する(public メンバ関数) | |
監視 | |
| コルーチンが完了したかチェックする (public メンバ関数) | |
| ハンドルがコルーチンを表すかチェックする (public メンバ関数) | |
制御 | |
| コルーチンの実行を再開する (public メンバ関数) | |
| コルーチンを破棄する (public メンバ関数) | |
Promiseへのアクセス | |
| コルーチンのpromiseにアクセスする (public メンバ関数) | |
| [static] |
コルーチンのpromiseオブジェクトから coroutine_handle を作成する(public static メンバ関数) |
エクスポート/インポート | |
| 基となるアドレス、すなわちコルーチンを裏付けるポインタをエクスポートする (public メンバ関数) | |
| [static] |
ポインタからコルーチンをインポートする (public static メンバ関数) |
[編集] 非メンバ関数
| (C++20) |
2つの coroutine_handle オブジェクトを比較する(関数) |
[編集] ヘルパークラス
| std::coroutine_handle のハッシュサポート (クラステンプレートの特殊化) |
[編集] 注釈
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(クラステンプレート) |