Namensräume
Varianten
Aktionen

std::noop_coroutine

Von cppreference.com
< cpp‎ | coroutine
 
 
Dienstprogramm-Bibliotheken
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Coroutine-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

Allgemeine Hilfsmittel
Relationale Operatoren (in C++20 veraltet)
 
Coroutine support
Coroutine traits
Coroutine handle
No-op coroutines
noop_coroutine
(C++20)
Trivial awaitables
Range generators
(C++23)
 
Definiert in Header <coroutine>
std::noop_coroutine_handle noop_coroutine() noexcept;
(seit C++20)

Gibt ein Koroutinen-Handle zurück, das auf eine No-Op-Koroutine verweist.

Wenn bereits ein Koroutinenzustand einer No-Op-Koroutine existiert, ist es undefiniert, ob ein nachfolgender Aufruf von noop_coroutine ein zuvor erhaltenes Koroutinen-Handle zurückgibt oder ein Koroutinen-Handle, das auf einen neuen Koroutinenzustand einer No-Op-Koroutine verweist.

Inhalt

[edit] Parameter

(keine)

[edit] Rückgabewert

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

[edit] Hinweise

Rückgabewerte von unterschiedlichen Aufrufen von noop_coroutine können gleich sein oder nicht.

noop_coroutine kann ein noop_coroutine_handle zurückgeben, das auf ein Koroutinenzustandsobjekt verweist, ohne eine Koroutine zu starten.

[edit] 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 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';
}

Ausgabe

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

[edit] Siehe auch

Wird für Coroutinen ohne beobachtbare Effekte verwendet
(Klasse) [bearbeiten]
std::coroutine_handle<std::noop_coroutine_promise>, das auf eine No-Op-Koroutine verweisen soll.
(typedef) [bearbeiten]