[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/coroutine/coroutine_handle [Back]  [Original]

std::coroutine_handle, std::noop_coroutine_handle cppreference.com
cppreference.com

std::coroutine_handle, std::noop_coroutine_handle

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
 
 
<tbody> </tbody>
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.
2) std::coroutine_handle<void> . .
3) std::coroutine_handle<std::noop_coroutine_promise> . .

std::coroutine_handle TriviallyCopyable.

, std::coroutine_handle .

ptr (private) void* .
( *)

-

(C++20)
coroutine_handle
(public -) []
(C++20)
coroutine_handle
(public -) []
coroutine_handle
(public -) []
(C++20)
,
(public -) []
,
(public -) []

(public -) []
(C++20)

(public -) []
(C++20)

(public -) []
[static] (C++20)
coroutine_handle
(public static -) []
/
(C++20)
, ,
(public -) []
[static] (C++20)

(public static -) []

,

coroutine_handle
() []

std::coroutine_handle
( ) []

A 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 {};
        }
        //  co_await   .
        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;
    }

    //      .
    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++:

LWG 3460 C++20 coroutine_handle

(C++23)
view,
( ) []

Web Proxy Viewer  |  New URL  |  Original Page