Namespaces
Variants

std:: noop_coroutine

From cppreference.net
Utilities library
Coroutine support
Coroutine traits
Coroutine handle
No-op coroutines
noop_coroutine
(C++20)
Trivial awaitables
Range generators
(C++23)
Definiert im Header <coroutine>
std:: noop_coroutine_handle noop_coroutine ( ) noexcept ;
(seit C++20)

Gibt ein Coroutine-Handle zurück, das sich auf eine No-Op-Coroutine bezieht.

Falls bereits ein Coroutine-Zustand einer No-Op-Coroutine existiert, ist nicht spezifiziert, ob ein nachfolgender Aufruf von noop_coroutine ein zuvor erhaltenes Coroutine-Handle zurückgibt oder ein Coroutine-Handle, das sich auf einen neuen Coroutine-Zustand einer No-Op-Coroutine bezieht.

Inhaltsverzeichnis

Parameter

(keine)

Rückgabewert

Ein std::noop_coroutine_handle , das auf eine No-Op-Coroutine verweist.

Hinweise

Rückgabewerte aus verschiedenen Aufrufen von noop_coroutine können gleich sein oder auch nicht gleich sein.

noop_coroutine kann nur ein noop_coroutine_handle zurückgeben, das sich auf ein Coroutine-Zustandsobjekt bezieht, ohne eine Coroutine zu starten.

Beispiel

#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 wird aufgerufen, wenn die Ausführung des
                // aktuelle Coroutine (referenziert durch 'h') wird gleich beendet.
                // Wenn die aktuelle Coroutine von einer anderen Coroutine via
                // co_await get_task(), ein Handle zu dieser Coroutine wurde gespeichert
                // wie h.promise().previous. In diesem Fall, geben Sie den Handle zur Fortsetzung zurück
                // die vorherige Coroutine.
                // Andernfalls wird noop_coroutine() zurückgegeben, dessen Fortsetzung nichts bewirkt.
                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';
}

Ausgabe:

in test()
in get_random()
in get_random()
8

Siehe auch

wird für Coroutinen ohne beobachtbare Effekte verwendet
(Klasse)
std:: coroutine_handle < std:: noop_coroutine_promise > , dient zur Referenzierung einer No-Op-Coroutine
(Typdefinition)