std:: noop_coroutine
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Coroutine support
| Coroutine traits | ||||
|
(C++20)
|
||||
| Coroutine handle | ||||
|
(C++20)
|
||||
| No-op coroutines | ||||
|
(C++20)
|
||||
|
noop_coroutine
(C++20)
|
||||
| Trivial awaitables | ||||
|
(C++20)
|
||||
|
(C++20)
|
||||
| Range generators | ||||
|
(C++23)
|
|
定义于头文件
<coroutine>
|
||
|
std::
noop_coroutine_handle
noop_coroutine
(
)
noexcept
;
|
(C++20 起) | |
返回一个引用无操作协程的协程句柄。
如果已经存在一个无操作协程的状态,则后续对
noop_coroutine
的调用是返回先前获取的协程句柄,还是引用一个新的无操作协程状态的协程句柄,这一点是未指定的。
目录 |
参数
(无)
返回值
一个 std::noop_coroutine_handle 指向无操作协程的句柄。
注释
来自不同调用
noop_coroutine
的返回值可能相等也可能不相等。
noop_coroutine
仅可返回指向协程状态对象的
noop_coroutine_handle
,而不会启动协程。
示例
运行此代码
#include <coroutine> #include <iostream> #include <utility> template<class T> struct task { struct promise_type { auto get_return_object() { return task(std::coroutine_handle<promise_type>::from_promise(*this)); } std::suspend_always initial_suspend() { return {}; } struct final_awaiter { bool await_ready() noexcept { return false; } void await_resume() noexcept {} std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> h) noexcept { // final_awaiter::await_suspend 在当前协程(由 'h' 引用)即将执行完毕时被调用。 // 如果当前协程是通过 co_await get_task() 由另一个协程恢复的, // 则该协程的句柄已存储为 h.promise().previous。 // 在这种情况下,返回该句柄以恢复先前的协程。 // 否则,返回 noop_coroutine(),其恢复不执行任何操作。 if (auto previous = h.promise().previous; previous) return previous; else return std::noop_coroutine(); } }; final_awaiter final_suspend() noexcept { return {}; } void unhandled_exception() { throw; } void return_value(T value) { result = std::move(value); } T result; std::coroutine_handle<> previous; }; task(std::coroutine_handle<promise_type> h) : coro(h) {} task(task&& t) = delete; ~task() { coro.destroy(); } struct awaiter { bool await_ready() { return false; } T await_resume() { return std::move(coro.promise().result); } auto await_suspend(std::coroutine_handle<> h) { coro.promise().previous = h; return coro; } std::coroutine_handle<promise_type> coro; }; awaiter operator co_await() { return awaiter{coro}; } T operator()() { coro.resume(); return std::move(coro.promise().result); } private: std::coroutine_handle<promise_type> coro; }; task<int> get_random() { std::cout << "in get_random()\n"; co_return 4; } task<int> test() { task<int> v = get_random(); task<int> u = get_random(); std::cout << "in test()\n"; int x = (co_await v + co_await u); co_return x; } int main() { task<int> t = test(); int result = t(); std::cout << result << '\n'; }
输出:
in test() in get_random() in get_random() 8
参见
|
(C++20)
|
用于无可见效果的协程
(类) |
|
(C++20)
|
std::
coroutine_handle
<
std::
noop_coroutine_promise
>
,用于引用无操作协程
(类型别名) |