名稱空間
變體
操作

std::noop_coroutine

來自 cppreference.com
< cpp‎ | 協程
 
 
 
協程支援
協程特質
協程控制代碼
無操作協程
noop_coroutine
(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 is called when the execution of the
                // current coroutine (referred to by 'h') is about to finish.
                // If the current coroutine was resumed by another coroutine via
                // co_await get_task(), a handle to that coroutine has been stored
                // as h.promise().previous. In that case, return the handle to resume
                // the previous coroutine.
                // Otherwise, return noop_coroutine(), whose resumption does nothing.
 
                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

[編輯] 另請參見

用於沒有可觀察效果的協程
(類) [編輯]
std::coroutine_handle<std::noop_coroutine_promise>,意圖指向一個無操作協程
(型別定義) [編輯]