{"id":478643,"date":"2026-05-05T11:16:11","date_gmt":"2026-05-05T11:16:11","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=478643"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=478643","title":{"rendered":"\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 Task \u0434\u043b\u044f C++20 coroutines: \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 coroutine state"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>execution_core::Task<\/code>, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 C++20.<\/p>\n<p>Coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 C++20 \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432 \u0442\u0435\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>co_await<\/code>, <code>co_yield<\/code> \u0438\u043b\u0438 <code>co_return<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>. \u0414\u043b\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 promise type \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 <code>std::coroutine_traits<\/code>. [1]<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0437\u0430\u0434\u0430\u0451\u0442 <code>promise_type<\/code> \u0434\u043b\u044f <code>Task&lt;void&gt;<\/code> \u0438 <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u0434\u0430\u0451\u0442 \u0434\u0432\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438: <code>StartSuspended<\/code> \u0438 <code>StartImmediately<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0445\u0440\u0430\u043d\u0438\u0442 <code>std::coroutine_handle&lt;promise_type&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442 coroutine state \u0447\u0435\u0440\u0435\u0437 <code>coroutine_handle::destroy()<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 <code>std::exception_ptr<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 <code>std::optional&lt;T&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u0440\u043c\u0438\u043d \u00ab\u043c\u043e\u0434\u0443\u043b\u044c\u00bb \u0434\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435. \u041a\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f header-only \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c. \u042d\u0442\u043e \u043d\u0435 C++20 module unit, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043d\u0451\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>#pragma once<\/code>, \u0430 \u043d\u0435 <code>export module<\/code>.<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u044b\u0431\u0440\u0430\u043d \u0442\u0430\u043a\u043e\u0439 Task?<\/h3>\n<p>C++20 coroutines \u0437\u0430\u0434\u0430\u044e\u0442 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f coroutine body, \u043d\u043e \u043d\u0435 \u0437\u0430\u0434\u0430\u044e\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438. Coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c return type. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e return type \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0447\u0435\u0440\u0435\u0437 <code>std::coroutine_traits<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 <code>promise_type<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0442\u0438\u043f <code>Task&lt;T, StartPolicy&gt;<\/code> \u043d\u0443\u0436\u0435\u043d \u043a\u0430\u043a \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>\u0442\u0438\u043f promise object;<\/p>\n<\/li>\n<li>\n<p>\u043e\u0431\u044a\u0435\u043a\u0442, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0438\u0437 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>\u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>std::coroutine_handle<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0441\u043f\u043e\u0441\u043e\u0431 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c lifetime coroutine state;<\/p>\n<\/li>\n<li>\n<p>\u043c\u0435\u0441\u0442\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 coroutine body \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u0435\u0437 \u0442\u0430\u043a\u043e\u0433\u043e return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f. \u042f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u043e\u0437\u0434\u0430\u0451\u0442 coroutine state \u0438 promise object, \u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u0434\u0443 \u043d\u0443\u0436\u0435\u043d \u043e\u0431\u044a\u0435\u043a\u0442, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u0442\u043e\u0442 state \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>Task&lt;T, StartPolicy&gt;<\/code>. \u041e\u043d \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, promise object \u0438 coroutine handle \u0432 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043d\u0435\u0442 \u043f\u0440\u0435\u0442\u0435\u043d\u0437\u0438\u0438 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c: \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438. \u0417\u0434\u0435\u0441\u044c \u043e\u043d \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e return object \u0434\u043b\u044f C++20 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0442\u0435\u043c, \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u044f\u0432\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a \u0441\u0432\u044f\u0437\u0430\u043d\u044b <code>promise_type<\/code>, <code>std::coroutine_handle<\/code>, <code>initial_suspend()<\/code>, <code>final_suspend()<\/code> \u0438 lifetime coroutine state.<\/p>\n<h3>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434<\/h3>\n<details class=\"spoiler\">\n<summary>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 Task<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#pragma once#include &lt;coroutine&gt;#include &lt;exception&gt;#include &lt;optional&gt;#include &lt;type_traits&gt;#include &lt;utility&gt;#include &lt;cassert&gt;namespace execution_core {struct StartImmediately {};struct StartSuspended {};template&lt;typename T = void, typename StartPolicy = StartSuspended&gt;class Task {    static_assert(        std::is_same_v&lt;StartPolicy, StartImmediately&gt; ||        std::is_same_v&lt;StartPolicy, StartSuspended&gt;    );private:    template&lt;typename Promise&gt;    struct base_promise {        std::exception_ptr exception;        Task get_return_object() noexcept {            return Task{                std::coroutine_handle&lt;Promise&gt;::from_promise(                    static_cast&lt;Promise&amp;&gt;(*this)                )            };        }        auto initial_suspend() noexcept {            if constexpr (std::is_same_v&lt;StartPolicy, StartImmediately&gt;) {                return std::suspend_never{};            } else {                return std::suspend_always{};            }        }        std::suspend_always final_suspend() noexcept {            return {};        }        void unhandled_exception() {            exception = std::current_exception();        }    };    struct void_promise final : base_promise&lt;void_promise&gt; {        void return_void() noexcept {}    };    template&lt;typename U&gt;    struct value_promise final : base_promise&lt;value_promise&lt;U&gt;&gt; {        std::optional&lt;U&gt; result;        template&lt;typename V&gt;        void return_value(V&amp;&amp; value) {            result.emplace(std::forward&lt;V&gt;(value));        }    };public:    using promise_type = std::conditional_t&lt;        std::is_void_v&lt;T&gt;,        void_promise,        value_promise&lt;T&gt;    &gt;;    using handle_type = std::coroutine_handle&lt;promise_type&gt;;public:    Task() noexcept = default;    explicit Task(handle_type handle) noexcept        : handle_(handle)    {}    Task(const Task&amp;) = delete;    Task&amp; operator=(const Task&amp;) = delete;    Task(Task&amp;&amp; other) noexcept        : handle_(std::exchange(other.handle_, {}))    {}    Task&amp; operator=(Task&amp;&amp; other) noexcept {        if (this != &amp;other) {            destroy();            handle_ = std::exchange(other.handle_, {});        }        return *this;    }    ~Task() {        destroy();    }    void start() {        if constexpr (std::is_same_v&lt;StartPolicy, StartSuspended&gt;) {            if (handle_ &amp;&amp; !handle_.done()) {                handle_.resume();            }        }    }    bool done() const noexcept {        return !handle_ || handle_.done();    }    void rethrow_if_exception() {        if (handle_ &amp;&amp; handle_.promise().exception) {            std::rethrow_exception(handle_.promise().exception);        }    }    handle_type native_handle() const noexcept {        return handle_;    }    explicit operator bool() const noexcept {        return static_cast&lt;bool&gt;(handle_);    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    U&amp; result() &amp; {        assert(handle_);        assert(handle_.done());        rethrow_if_exception();        assert(handle_.promise().result.has_value());        return *handle_.promise().result;    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    const U&amp; result() const&amp; {        assert(handle_);        assert(handle_.done());        if (handle_.promise().exception) {            std::rethrow_exception(handle_.promise().exception);        }        assert(handle_.promise().result.has_value());        return *handle_.promise().result;    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    U&amp;&amp; result() &amp;&amp; {        assert(handle_);        assert(handle_.done());        rethrow_if_exception();        assert(handle_.promise().result.has_value());        return std::move(*handle_.promise().result);    }private:    void destroy() noexcept {        if (handle_) {            handle_.destroy();            handle_ = {};        }    }private:    handle_type handle_{};};} \/\/ namespace execution_core<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<\/div>\n<\/details>\n<h3>\u041e\u0431\u043b\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 Task<\/h3>\n<p><code>Task&lt;T, StartPolicy&gt;<\/code> \u0437\u0430\u0434\u0430\u0451\u0442 return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>Return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 <code>promise.get_return_object()<\/code>. \u042d\u0442\u043e\u0442 \u0432\u044b\u0437\u043e\u0432 \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u0443 <code>promise.initial_suspend()<\/code> \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430 [1].<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>Task<\/code> \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f scheduler, event loop \u0438\u043b\u0438 thread pool. \u041e\u043d \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 coroutine handle.<\/p>\n<p>\u041e\u0431\u043b\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <code>Task<\/code> \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>promise_type<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 <code>std::coroutine_handle&lt;promise_type&gt;<\/code> \u0438\u0437 promise object;<\/p>\n<\/li>\n<li>\n<p>\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 coroutine handle;<\/p>\n<\/li>\n<li>\n<p>\u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 coroutine state;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0434\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 promise object.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0422\u0438\u043f\u044b \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430<\/h3>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0434\u0432\u0430 \u043f\u0443\u0441\u0442\u044b\u0445 \u0442\u0438\u043f\u0430: <code>StartImmediately<\/code> \u0438 <code>StartSuspended<\/code>. \u041e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 <code>StartPolicy<\/code>.<\/p>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0437\u0430\u0434\u0430\u043d\u043e \u0447\u0435\u0440\u0435\u0437 <code>static_assert<\/code>: <code>StartPolicy<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043b\u0438\u0431\u043e <code>StartImmediately<\/code>, \u043b\u0438\u0431\u043e <code>StartSuspended<\/code>.<\/p>\n<p>\u0422\u0438\u043f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u2014 <code>StartSuspended<\/code>. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, <code>Task&lt;T&gt;<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <code>Task&lt;T, StartSuspended&gt;<\/code>, \u0430 <code>Task&lt;&gt;<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <code>Task&lt;void, StartSuspended&gt;<\/code>.<\/p>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0444\u043e\u0440\u043c\u044b return type coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<ul>\n<li>\n<p>\u0434\u043b\u044f <code>void<\/code>-\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430: <code>Task&lt;&gt;<\/code>, <code>Task&lt;void&gt;<\/code>, <code>Task&lt;void, StartSuspended&gt;<\/code>, <code>Task&lt;void, StartImmediately&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438 <code>T != void<\/code>: <code>Task&lt;T&gt;<\/code>, <code>Task&lt;T, StartSuspended&gt;<\/code>, <code>Task&lt;T, StartImmediately&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b void- \u0438 value-\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b promise type<\/h3>\n<p>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 <code>co_return<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u0438\u043c \u0442\u0438\u043f\u043e\u043c <code>Task<\/code>, \u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438 coroutine promise.<\/p>\n<p>\u0414\u043b\u044f <code>co_return;<\/code> \u0438\u043b\u0438 <code>co_return<\/code> \u0441 operand \u0442\u0438\u043f\u0430 <code>void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>p.return_void()<\/code>.<\/p>\n<p>\u0414\u043b\u044f <code>co_return expr<\/code>, \u0433\u0434\u0435 operand \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f braced-init-list \u0438\u043b\u0438 expression non-void type, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>p.return_value(expr-or-braced-init-list)<\/code>.<\/p>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0435\u0441\u043b\u0438 \u0432 scope promise type \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u044b \u0438\u043c\u0435\u043d\u0430 <code>return_void<\/code> \u0438 <code>return_value<\/code>, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f ill-formed [1].<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0434\u0438\u043d \u043e\u0431\u0449\u0438\u0439 promise type \u0441 \u043e\u0431\u043e\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u044d\u0442\u043e\u043c\u0443 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044e.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0431\u043e\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">using promise_type = std::conditional_t&lt;    std::is_void_v&lt;T&gt;,    void_promise,    value_promise&lt;T&gt;&gt;;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435:<\/p>\n<pre><code class=\"cpp\">Task&lt;void&gt;              -&gt; void_promiseTask&lt;T&gt;, T != void      -&gt; value_promise&lt;T&gt;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f <code>Task&lt;void&gt;<\/code> \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 promise type \u0441 <code>return_void()<\/code>, \u0430 \u0434\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 promise type \u0441 <code>return_value(...)<\/code>.<\/p>\n<h3>base_promise<\/h3>\n<p><code>void_promise<\/code> \u0438 <code>value_promise&lt;T&gt;<\/code> \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <code>co_return<\/code>.<\/p>\n<p>\u041e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u043d\u0438\u0445 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f <code>get_return_object()<\/code>, <code>initial_suspend()<\/code>, <code>final_suspend()<\/code>, <code>unhandled_exception()<\/code> \u0438 \u043f\u043e\u043b\u0435 <code>exception<\/code>.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0430 \u0432 <code>base_promise&lt;Promise&gt;<\/code>. \u042d\u0442\u043e \u0443\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 promise object, \u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 promise types: <code>void_promise<\/code> \u0438 <code>value_promise&lt;T&gt;<\/code>.<\/p>\n<p><code>base_promise&lt;Promise&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0438\u043f promise \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>Promise<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>std::coroutine_handle&lt;Promise&gt;::from_promise(...)<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 promise object [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">[3]<\/a>.<\/p>\n<p>\u0412 <code>get_return_object()<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>static_cast&lt;Promise&amp;&gt;(*this)<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f coroutine handle \u0447\u0435\u0440\u0435\u0437 <code>std::coroutine_handle&lt;Promise&gt;::from_promise(...)<\/code>.<\/p>\n<p>\u0414\u043b\u044f <code>from_promise<\/code> \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0437\u0430\u0434\u0430\u0451\u0442 \u043f\u043e\u0441\u0442\u0443\u0441\u043b\u043e\u0432\u0438\u0435 <code>addressof(h.promise()) == addressof(p)<\/code>, \u0433\u0434\u0435 <code>p<\/code> \u2014 promise object, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d handle [1].<\/p>\n<h3>\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f coroutine return object<\/h3>\n<p>\u0414\u043b\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 return type <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>Task&lt;T&gt;::promise_type<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c <code>value_promise&lt;T&gt;<\/code>.<\/p>\n<p>\u0414\u043b\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 return type <code>Task&lt;void&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>Task&lt;void&gt;::promise_type<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c <code>void_promise<\/code>.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 C++20:<\/p>\n<ol>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f;<\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f coroutine state;<\/p>\n<\/li>\n<li>\n<p>\u0432 coroutine state \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f promise object;<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>promise.get_return_object()<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>get_return_object()<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 <code>Task<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 <code>std::coroutine_handle&lt;promise_type&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>promise.initial_suspend()<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 <code>initial_suspend()<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p>\u042d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 coroutine body, \u0433\u0434\u0435 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f return object \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <code>co_await promise.initial_suspend()<\/code> [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<h3>StartPolicy<\/h3>\n<p>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 coroutine body \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c <code>promise.initial_suspend()<\/code>.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0430 \u0440\u0435\u0436\u0438\u043c\u0430:<\/p>\n<ul>\n<li>\n<p><code>StartSuspended<\/code> \u0437\u0430\u0434\u0430\u0451\u0442 <code>initial_suspend() -&gt; std::suspend_always<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>StartImmediately<\/code> \u0437\u0430\u0434\u0430\u0451\u0442 <code>initial_suspend() -&gt; std::suspend_never<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><code>std::suspend_always<\/code> \u0437\u0430\u0434\u0430\u0451\u0442 awaitable object, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e <code>await_ready()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>false<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_always\" rel=\"noopener noreferrer nofollow\">[4]<\/a>. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f <code>Task&lt;T, StartSuspended&gt;<\/code> coroutine body \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438. \u0417\u0430\u043f\u0443\u0441\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u043e \u0447\u0435\u0440\u0435\u0437 <code>task.start()<\/code>.<\/p>\n<p>\u0422\u0430\u043a\u043e\u0439 \u0440\u0435\u0436\u0438\u043c \u043d\u0443\u0436\u0435\u043d, \u043a\u043e\u0433\u0434\u0430 coroutine handle \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d, \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d scheduler-\u0443, \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e coroutine body \u0434\u043e\u043b\u0436\u043d\u0430 \u043d\u0430\u0447\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p><code>std::suspend_never<\/code> \u0437\u0430\u0434\u0430\u0451\u0442 awaitable object, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e <code>await_ready()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>true<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_never\" rel=\"noopener noreferrer nofollow\">[5]<\/a>. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f <code>Task&lt;T, StartImmediately&gt;<\/code> coroutine body \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f return object.<\/p>\n<p><code>StartPolicy<\/code> \u0432 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0434\u0430\u0451\u0442 \u043d\u0435 runtime-\u0444\u043b\u0430\u0433, \u0430 compile-time \u0432\u044b\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 <code>initial_suspend()<\/code>.<\/p>\n<h3>start()<\/h3>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>start()<\/code> \u0438\u043c\u0435\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f <code>Task&lt;T, StartSuspended&gt;<\/code>.<\/p>\n<p>\u0414\u043b\u044f <code>Task&lt;T, StartImmediately&gt;<\/code> \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 <code>if constexpr<\/code> \u0442\u0435\u043b\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 <code>handle_.resume()<\/code>.<\/p>\n<p>\u0414\u043b\u044f <code>StartSuspended<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 <code>handle_<\/code> \u0438 <code>!handle_.done()<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>handle_.resume()<\/code>.<\/p>\n<p><code>coroutine_handle::resume()<\/code> \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 coroutine, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f coroutine handle [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">[3]<\/a>.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e: <code>coroutine_handle::done()<\/code> \u0438\u043c\u0435\u0435\u0442 precondition: handle \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 suspended coroutine. Task::done() \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 !handle_.done() \u0432 start() \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e coroutine \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 suspended state.<\/p>\n<p><code>resume()<\/code> \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f handle, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 suspended coroutine, \u043f\u0440\u0438\u0447\u0451\u043c coroutine \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 final suspend point.<\/p>\n<h3>\u0420\u043e\u043b\u044c std::suspend_always \u0432 final_suspend() \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u041f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 coroutine body \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <code>co_await promise.final_suspend()<\/code> [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>final_suspend()<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>std::suspend_always<\/code>.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <code>co_return<\/code> \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 promise object \u0432 <code>handle_.promise().result<\/code>. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 promise object \u0432 <code>handle_.promise().exception<\/code>.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>result()<\/code> \u0447\u0438\u0442\u0430\u0435\u0442 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f coroutine body. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 coroutine state \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f coroutine body \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 <code>Task<\/code> \u0432\u044b\u0437\u043e\u0432\u0435\u0442 <code>handle_.destroy()<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 coroutine state \u0431\u044b\u043b \u0431\u044b \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>result()<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f \u043a promise object \u0447\u0435\u0440\u0435\u0437 <code>handle_.promise()<\/code> \u0431\u044b\u043b \u0431\u044b \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d.<\/p>\n<h3>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043f\u043e\u0441\u043b\u0435 co_return<\/h3>\n<p>\u0414\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>value_promise&lt;T&gt;<\/code>, \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f <code>std::optional&lt;T&gt; result<\/code>.<\/p>\n<p><code>std::optional&lt;T&gt;<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u0438\u0431\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 <code>T<\/code>, \u043b\u0438\u0431\u043e \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional\" rel=\"noopener noreferrer nofollow\">[7]<\/a>.<\/p>\n<p>\u0414\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <code>co_return value<\/code> \u043e\u0431\u044a\u0435\u043a\u0442 <code>result<\/code> \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 <code>co_return value<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>promise.return_value(value)<\/code>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0432\u044b\u0437\u043e\u0432\u0443 <code>result.emplace(std::forward&lt;V&gt;(value))<\/code>.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, <code>std::optional&lt;T&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a storage \u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 coroutine state, \u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 <code>co_return<\/code>.<\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 void-\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/h3>\n<p>\u0414\u043b\u044f <code>void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>void_promise<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 <code>return_void()<\/code>.<\/p>\n<p>\u0414\u043b\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 return type <code>Task&lt;void&gt;<\/code> \u0438\u043b\u0438 <code>Task&lt;&gt;<\/code> \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 <code>co_return;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>return_void()<\/code> [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f.<\/p>\n<h3>\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443<\/h3>\n<p>\u041c\u0435\u0442\u043e\u0434\u044b <code>result()<\/code> \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code>.<\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u043d\u043e \u0447\u0435\u0440\u0435\u0437 constraint:<\/p>\n<pre><code class=\"cpp\">template&lt;typename U = T&gt;requires (!std::is_void_v&lt;U&gt;)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f <code>Task&lt;void&gt;<\/code> \u043c\u0435\u0442\u043e\u0434\u044b <code>result()<\/code> \u043d\u0435 \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 overload resolution, \u0430 \u0434\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0442\u0440\u0438 overload:<\/p>\n<pre><code class=\"cpp\">U&amp;       result() &amp;;const U&amp; result() const&amp;;U&amp;&amp;      result() &amp;&amp;;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u043d\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f ref-qualifier-\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0447\u043b\u0435\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u0434\u043b\u044f lvalue-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Task&lt;T&gt; task<\/code> \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f <code>U&amp; result() &amp;<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f const lvalue-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>const Task&lt;T&gt; task<\/code> \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f <code>const U&amp; result() const&amp;<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f rvalue-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>std::move(task).result()<\/code> \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f <code>U&amp;&amp; result() &amp;&amp;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0423\u0441\u043b\u043e\u0432\u0438\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 result() \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">assert(handle_);assert(handle_.done());assert(handle_.promise().result.has_value());<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, <code>result()<\/code> \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d \u043d\u0430 \u0432\u044b\u0437\u043e\u0432 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f coroutine body. \u041f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 <code>rethrow_if_exception()<\/code>.<\/p>\n<h3>\u0420\u043e\u043b\u044c std::exception_ptr \u0432 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0432\u044b\u0448\u0435\u0434\u0448\u0435\u0435 \u0438\u0437 coroutine body, \u043d\u0435 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0440\u0443\u0436\u0443 \u043e\u0431\u044b\u0447\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0432 \u0442\u043e\u0447\u043a\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>promise.unhandled_exception()<\/code> [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>unhandled_exception()<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 <code>exception = std::current_exception()<\/code>.<\/p>\n<p>\u041f\u043e\u0437\u0436\u0435 <code>result()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>rethrow_if_exception()<\/code>, \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>std::rethrow_exception(...)<\/code>.<\/p>\n<p><code>std::exception_ptr<\/code> \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0442\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/error\/exception_ptr\" rel=\"noopener noreferrer nofollow\">[8]<\/a>.<\/p>\n<p>\u0414\u043b\u044f <code>Task&lt;T&gt;<\/code> \u043f\u0440\u0438 <code>T != void<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 <code>result()<\/code>.<\/p>\n<p>\u0414\u043b\u044f <code>Task&lt;void&gt;<\/code> \u043c\u0435\u0442\u043e\u0434\u0430 <code>result()<\/code> \u043d\u0435\u0442; \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u044b\u043c \u0432\u044b\u0437\u043e\u0432\u043e\u043c <code>rethrow_if_exception()<\/code> \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f coroutine body.<\/p>\n<h3>\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435 coroutine handle<\/h3>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f <code>handle_type handle_<\/code>, \u0433\u0434\u0435 <code>handle_type<\/code> \u2014 \u044d\u0442\u043e <code>std::coroutine_handle&lt;promise_type&gt;<\/code>.<\/p>\n<p><code>std::coroutine_handle<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f handle-\u0442\u0438\u043f\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 coroutine state, \u043d\u043e \u0441\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u043d\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 RAII-\u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c [3].<\/p>\n<p>\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0451\u0442\u0441\u044f \u0441\u0430\u043c\u0438\u043c <code>Task<\/code>: \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e, \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e, \u0430 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>destroy()<\/code>.<\/p>\n<p>\u041a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u043e\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Task<\/code> \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 coroutine handle \u0438 \u043e\u0431\u0430 \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>destroy()<\/code> \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e coroutine state.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0438 handle \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443, \u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 <code>std::exchange(other.handle_, {})<\/code>.<\/p>\n<p>\u0421\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435: \u0432 \u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 coroutine state \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043e\u0434\u043d\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c <code>Task<\/code>.<\/p>\n<h3>\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0434\u043b\u044f \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u044f coroutine state<\/h3>\n<p><code>destroy()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0433\u043e handle. \u0412\u043d\u0443\u0442\u0440\u0438 <code>destroy()<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <code>handle_.destroy()<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e <code>handle_<\/code> \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0443\u0441\u0442\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p><code>coroutine_handle::destroy()<\/code> \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442 coroutine state, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f handle [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">[3]<\/a>.<\/p>\n<p>\u0412\u044b\u0437\u043e\u0432 <code>handle_.destroy()<\/code> \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 <code>Task<\/code> \u0438\u043c\u0435\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445:<\/p>\n<ul>\n<li>\n<p><code>handle_<\/code> \u043d\u0435 \u0431\u044b\u043b \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043f\u0438\u044e, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0438\u0437 <code>native_handle()<\/code>;<\/p>\n<\/li>\n<li>\n<p>coroutine \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e;<\/p>\n<\/li>\n<li>\n<p>coroutine \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 suspended state.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u043e, \u0447\u0442\u043e \u0432\u044b\u0437\u043e\u0432 <code>destroy()<\/code> \u0434\u043b\u044f coroutine, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 suspended state, \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a undefined behavior [1].<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 <code>Task<\/code>, \u0432\u044b\u0437\u043e\u0432\u044b <code>resume()<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 handle, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0435\u0440\u0435\u0437 <code>native_handle()<\/code>, \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u044b \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c.<\/p>\n<h3>native_handle()<\/h3>\n<p><code>Task<\/code> \u0441\u0430\u043c \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f scheduler-\u043e\u043c.<\/p>\n<p>\u041d\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 scheduler \u0438\u043b\u0438 event loop \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c coroutine handle, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0438 \u043f\u043e\u0437\u0436\u0435 \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>resume()<\/code>.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434 <code>native_handle()<\/code>. \u041e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e <code>std::coroutine_handle&lt;promise_type&gt;<\/code>, \u043d\u043e \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 coroutine state.<\/p>\n<p>\u0423\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 coroutine state \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Task<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e <code>Task<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>handle_.destroy()<\/code>.<\/p>\n<h3>\u0413\u0440\u0430\u043d\u0438\u0446\u044b \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0437\u0430\u0434\u0430\u0451\u0442 return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 lifetime coroutine state, \u043d\u043e \u043d\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 awaiter protocol.<\/p>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 <code>await_ready()<\/code>, <code>await_suspend(...)<\/code>, <code>await_resume()<\/code> \u0438 <code>operator co_await()<\/code>.<\/p>\n<p>\u0412 C++ coroutine await-expression \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 awaiter protocol, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0439 <code>await_ready<\/code>, <code>await_suspend<\/code> \u0438 <code>await_resume<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">[2]<\/a>.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u0434\u0430\u0451\u0442 return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Task&lt;T&gt; f();<\/code>, \u043d\u043e \u043d\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f <code>co_await f()<\/code>.<\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>co_await Task&lt;T&gt;<\/code> \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 awaiter protocol.<\/p>\n<h3>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0445 \u0438 \u043d\u0435\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/h3>\n<p>\u0414\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>std::optional&lt;T&gt; result<\/code>.<\/p>\n<p><code>std::optional&lt;T&gt;<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 <code>T<\/code> \u043a\u0430\u043a contained value <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional\" rel=\"noopener noreferrer nofollow\">[7]<\/a>.<\/p>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u0430\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f value-\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 <code>T<\/code>, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 <code>std::optional&lt;T&gt;<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f well-formed \u0438 \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>result.emplace(std::forward&lt;V&gt;(value))<\/code> well-formed \u0434\u043b\u044f operand \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e <code>co_return<\/code>.<\/p>\n<p><code>Task&lt;T&amp;&gt;<\/code>, <code>Task&lt;T[]&gt;<\/code>, <code>Task&lt;function-type&gt;<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u043e\u0440\u043c\u044b, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 <code>std::optional&lt;T&gt;<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c contained value \u0442\u0438\u043f\u0430 <code>T<\/code>, \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f.<\/p>\n<h3>\u0421\u0432\u043e\u0434\u043a\u0430 \u0442\u0438\u043f\u043e\u0432<\/h3>\n<p>\u0414\u043b\u044f <code>void<\/code>-\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430:<\/p>\n<ul>\n<li>\n<p><code>Task&lt;&gt;<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <code>Task&lt;void, StartSuspended&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task&lt;void&gt;<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <code>Task&lt;void, StartSuspended&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task&lt;void, StartSuspended&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>void_promise<\/code>, <code>std::suspend_always<\/code>, <code>return_void()<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task&lt;void, StartImmediately&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>void_promise<\/code>, <code>std::suspend_never<\/code>, <code>return_void()<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438 <code>T != void<\/code>:<\/p>\n<ul>\n<li>\n<p><code>Task&lt;T&gt;<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d <code>Task&lt;T, StartSuspended&gt;<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task&lt;T, StartSuspended&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>value_promise&lt;T&gt;<\/code>, <code>std::suspend_always<\/code>, <code>return_value(...)<\/code>;<\/p>\n<\/li>\n<li>\n<p><code>Task&lt;T, StartImmediately&gt;<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>value_promise&lt;T&gt;<\/code>, <code>std::suspend_never<\/code>, <code>return_value(...)<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u0441\u0445\u0435\u043c\u0430:<\/p>\n<pre><code class=\"cpp\">Task&lt;&gt;                         -&gt; Task&lt;void, StartSuspended&gt;Task&lt;void&gt;                     -&gt; Task&lt;void, StartSuspended&gt;Task&lt;void, StartSuspended&gt;     -&gt; void_promise,     suspend_always, return_void()Task&lt;void, StartImmediately&gt;   -&gt; void_promise,     suspend_never,  return_void()Task&lt;T&gt;                        -&gt; Task&lt;T, StartSuspended&gt;, T != voidTask&lt;T, StartSuspended&gt;        -&gt; value_promise&lt;T&gt;, suspend_always, return_value(...)Task&lt;T, StartImmediately&gt;      -&gt; value_promise&lt;T&gt;, suspend_never,  return_value(...)<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<h3>\u0418\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430<\/h3>\n<p><code>execution_core::Task&lt;T, StartPolicy&gt;<\/code> \u2014 \u044d\u0442\u043e class template, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u0451\u0442 return object \u0434\u043b\u044f C++20 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>T<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 promise type: \u043f\u0440\u0438 <code>T == void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>void_promise<\/code>, \u043f\u0440\u0438 <code>T != void<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>value_promise&lt;T&gt;<\/code>.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>StartPolicy<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <code>initial_suspend()<\/code>: <code>StartSuspended<\/code> \u0434\u0430\u0451\u0442 <code>std::suspend_always<\/code>, \u0430 <code>StartImmediately<\/code> \u0434\u0430\u0451\u0442 <code>std::suspend_never<\/code>.<\/p>\n<p>Coroutine handle \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>std::coroutine_handle&lt;Promise&gt;::from_promise(...)<\/code> \u0438 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 <code>Task<\/code> [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">[3]<\/a>.<\/p>\n<p>Coroutine state \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u043c <code>Task<\/code> \u0447\u0435\u0440\u0435\u0437 <code>handle_.destroy()<\/code> \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0433\u043e handle [1]<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">[3]<\/a>.<\/p>\n<p>\u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>Task<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0435\u043c coroutine handle \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a promise object \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f coroutine body. \u041e\u043d \u043d\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 awaiter protocol \u0434\u043b\u044f <code>co_await Task&lt;T&gt;<\/code>.<\/p>\n<p>\u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 <code>done()<\/code>, <code>resume()<\/code> \u0438 <code>destroy()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u0445 coroutine, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u043c\u0435\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443.<\/p>\n<h3>\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438<\/h3>\n<ol>\n<li>\n<p>ISO\/IEC 14882:2020(E), \u0440\u0430\u0437\u0434\u0435\u043b\u044b: <\/p>\n<ul>\n<li>\n<p>Coroutine definitions;<\/p>\n<\/li>\n<li>\n<p>Coroutine promise;<\/p>\n<\/li>\n<li>\n<p>Coroutine handle;<\/p>\n<\/li>\n<li>\n<p>Coroutine state lifetime.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>cppreference: Coroutines <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/language\/coroutines<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::coroutine_handle<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/coroutine_handle<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::suspend_always<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_always\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_always<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::suspend_never<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_never\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/coroutine\/suspend_never<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::conditional<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/conditional\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/types\/conditional<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::optional<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional<\/a><\/p>\n<\/li>\n<li>\n<p>cppreference: <code>std::exception_ptr<\/code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/error\/exception_ptr\" rel=\"noopener noreferrer nofollow\">https:\/\/en.cppreference.com\/w\/cpp\/error\/exception_ptr<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1031644\/\">https:\/\/habr.com\/ru\/articles\/1031644\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 execution_core::Task, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 C++20.Coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 C++20 \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432 \u0442\u0435\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f co_await, co_yield \u0438\u043b\u0438 co_return [2]. \u0414\u043b\u044f coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 promise type \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 std::coroutine_traits. [1]\u0420\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:\u0437\u0430\u0434\u0430\u0451\u0442 promise_type \u0434\u043b\u044f Task&lt;void&gt; \u0438 Task&lt;T&gt; \u043f\u0440\u0438 T != void;\u0437\u0430\u0434\u0430\u0451\u0442 \u0434\u0432\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438: StartSuspended \u0438 StartImmediately;\u0445\u0440\u0430\u043d\u0438\u0442 std::coroutine_handle&lt;promise_type&gt;;\u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442 coroutine state \u0447\u0435\u0440\u0435\u0437 coroutine_handle::destroy();\u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 std::exception_ptr;\u0434\u043b\u044f Task&lt;T&gt; \u043f\u0440\u0438 T != void \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 std::optional&lt;T&gt;.\u0422\u0435\u0440\u043c\u0438\u043d \u00ab\u043c\u043e\u0434\u0443\u043b\u044c\u00bb \u0434\u0430\u043b\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435. \u041a\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f header-only \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c. \u042d\u0442\u043e \u043d\u0435 C++20 module unit, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043d\u0451\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f #pragma once, \u0430 \u043d\u0435 export module.\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u044b\u0431\u0440\u0430\u043d \u0442\u0430\u043a\u043e\u0439 Task?C++20 coroutines \u0437\u0430\u0434\u0430\u044e\u0442 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f coroutine body, \u043d\u043e \u043d\u0435 \u0437\u0430\u0434\u0430\u044e\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438. Coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c return type. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e return type \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0447\u0435\u0440\u0435\u0437 std::coroutine_traits \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 promise_type [2].\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0442\u0438\u043f Task&lt;T, StartPolicy&gt; \u043d\u0443\u0436\u0435\u043d \u043a\u0430\u043a \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f:\u0442\u0438\u043f promise object;\u043e\u0431\u044a\u0435\u043a\u0442, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0438\u0437 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438;\u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c std::coroutine_handle;\u0441\u043f\u043e\u0441\u043e\u0431 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c lifetime coroutine state;\u043c\u0435\u0441\u0442\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f;\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 coroutine body \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438.\u0411\u0435\u0437 \u0442\u0430\u043a\u043e\u0433\u043e return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f. \u042f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u043e\u0437\u0434\u0430\u0451\u0442 coroutine state \u0438 promise object, \u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u0434\u0443 \u043d\u0443\u0436\u0435\u043d \u043e\u0431\u044a\u0435\u043a\u0442, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u0442\u043e\u0442 state \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d.\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f Task&lt;T, StartPolicy&gt;. \u041e\u043d \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u044e, promise object \u0438 coroutine handle \u0432 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f.\u0412 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043d\u0435\u0442 \u043f\u0440\u0435\u0442\u0435\u043d\u0437\u0438\u0438 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c: \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438. \u0417\u0434\u0435\u0441\u044c \u043e\u043d \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e return object \u0434\u043b\u044f C++20 coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0442\u0435\u043c, \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u044f\u0432\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a \u0441\u0432\u044f\u0437\u0430\u043d\u044b promise_type, std::coroutine_handle, initial_suspend(), final_suspend() \u0438 lifetime coroutine state.\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 Task#pragma once#include &lt;coroutine&gt;#include &lt;exception&gt;#include &lt;optional&gt;#include &lt;type_traits&gt;#include &lt;utility&gt;#include &lt;cassert&gt;namespace execution_core {struct StartImmediately {};struct StartSuspended {};template&lt;typename T = void, typename StartPolicy = StartSuspended&gt;class Task {    static_assert(        std::is_same_v&lt;StartPolicy, StartImmediately&gt; ||        std::is_same_v&lt;StartPolicy, StartSuspended&gt;    );private:    template&lt;typename Promise&gt;    struct base_promise {        std::exception_ptr exception;        Task get_return_object() noexcept {            return Task{                std::coroutine_handle&lt;Promise&gt;::from_promise(                    static_cast&lt;Promise&amp;&gt;(*this)                )            };        }        auto initial_suspend() noexcept {            if constexpr (std::is_same_v&lt;StartPolicy, StartImmediately&gt;) {                return std::suspend_never{};            } else {                return std::suspend_always{};            }        }        std::suspend_always final_suspend() noexcept {            return {};        }        void unhandled_exception() {            exception = std::current_exception();        }    };    struct void_promise final : base_promise&lt;void_promise&gt; {        void return_void() noexcept {}    };    template&lt;typename U&gt;    struct value_promise final : base_promise&lt;value_promise&lt;U&gt;&gt; {        std::optional&lt;U&gt; result;        template&lt;typename V&gt;        void return_value(V&amp;&amp; value) {            result.emplace(std::forward&lt;V&gt;(value));        }    };public:    using promise_type = std::conditional_t&lt;        std::is_void_v&lt;T&gt;,        void_promise,        value_promise&lt;T&gt;    &gt;;    using handle_type = std::coroutine_handle&lt;promise_type&gt;;public:    Task() noexcept = default;    explicit Task(handle_type handle) noexcept        : handle_(handle)    {}    Task(const Task&amp;) = delete;    Task&amp; operator=(const Task&amp;) = delete;    Task(Task&amp;&amp; other) noexcept        : handle_(std::exchange(other.handle_, {}))    {}    Task&amp; operator=(Task&amp;&amp; other) noexcept {        if (this != &amp;other) {            destroy();            handle_ = std::exchange(other.handle_, {});        }        return *this;    }    ~Task() {        destroy();    }    void start() {        if constexpr (std::is_same_v&lt;StartPolicy, StartSuspended&gt;) {            if (handle_ &amp;&amp; !handle_.done()) {                handle_.resume();            }        }    }    bool done() const noexcept {        return !handle_ || handle_.done();    }    void rethrow_if_exception() {        if (handle_ &amp;&amp; handle_.promise().exception) {            std::rethrow_exception(handle_.promise().exception);        }    }    handle_type native_handle() const noexcept {        return handle_;    }    explicit operator bool() const noexcept {        return static_cast&lt;bool&gt;(handle_);    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    U&amp; result() &amp; {        assert(handle_);        assert(handle_.done());        rethrow_if_exception();        assert(handle_.promise().result.has_value());        return *handle_.promise().result;    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    const U&amp; result() const&amp; {        assert(handle_);        assert(handle_.done());        if (handle_.promise().exception) {            std::rethrow_exception(handle_.promise().exception);        }        assert(handle_.promise().result.has_value());        return *handle_.promise().result;    }    template&lt;typename U = T&gt;    requires (!std::is_void_v&lt;U&gt;)    U&amp;&amp; result() &amp;&amp; {        assert(handle_);        assert(handle_.done());        rethrow_if_exception();        assert(handle_.promise().result.has_value());        return std::move(*handle_.promise().result);    }private:    void destroy() noexcept {        if (handle_) {            handle_.destroy();            handle_ = {};        }    }private:    handle_type handle_{};};} \/\/ namespace execution_core\u041e\u0431\u043b\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 TaskTask&lt;T, StartPolicy&gt; \u0437\u0430\u0434\u0430\u0451\u0442 return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438.Return object coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 promise.get_return_object(). \u042d\u0442\u043e\u0442 \u0432\u044b\u0437\u043e\u0432 \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u0443 promise.initial_suspend() \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430 [1].\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Task \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f scheduler, event loop \u0438\u043b\u0438 thread pool. \u041e\u043d \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 coroutine handle.\u041e\u0431\u043b\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 Task \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432:\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 promise_type;\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 std::coroutine_handle&lt;promise_type&gt; \u0438\u0437 promise object;\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 coroutine handle;\u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 coroutine state;\u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0434\u043b\u044f Task&lt;T&gt; \u043f\u0440\u0438 T != void;\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 promise object.\u0422\u0438\u043f\u044b \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0412 \u043a\u043e\u0434\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0434\u0432\u0430 \u043f\u0443\u0441\u0442\u044b\u0445 \u0442\u0438\u043f\u0430: StartImmediately \u0438 StartSuspended. \u041e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 StartPolicy.\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0437\u0430\u0434\u0430\u043d\u043e \u0447\u0435\u0440\u0435\u0437 static_assert: StartPolicy \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043b\u0438\u0431\u043e StartImmediately, \u043b\u0438\u0431\u043e StartSuspended.\u0422\u0438\u043f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u2014 StartSuspended. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, Task&lt;T&gt; \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d Task&lt;T, StartSuspended&gt;, \u0430 Task&lt;&gt; \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d Task&lt;void, StartSuspended&gt;.\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0444\u043e\u0440\u043c\u044b return type coroutine-\u0444\u0443\u043d\u043a\u0446\u0438\u0439:\u0434\u043b\u044f void-\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430: Task&lt;&gt;, Task&lt;void&gt;, Task&lt;void, StartSuspended&gt;, Task&lt;void, StartImmediately&gt;;\u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438 T != void: Task&lt;T&gt;, Task&lt;T, StartSuspended&gt;, Task&lt;T, StartImmediately&gt;.\u041f\u043e\u0447\u0435\u043c\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b void- \u0438 value-\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b promise type\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 co_return \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u0438\u043c \u0442\u0438\u043f\u043e\u043c Task, \u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438 coroutine promise.\u0414\u043b\u044f co_return; \u0438\u043b\u0438 co_return \u0441 operand \u0442\u0438\u043f\u0430 void \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f p.return_void().\u0414\u043b\u044f co_return expr, \u0433\u0434\u0435 operand \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f braced-init-list \u0438\u043b\u0438 expression non-void type, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f p.return_value(expr-or-braced-init-list).\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0435\u0441\u043b\u0438 \u0432 scope promise type \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u044b \u0438\u043c\u0435\u043d\u0430 return_void \u0438 return_value, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f ill-formed [1].\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0434\u0438\u043d \u043e\u0431\u0449\u0438\u0439 promise type \u0441 \u043e\u0431\u043e\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u044d\u0442\u043e\u043c\u0443 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044e.\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0431\u043e\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:using promise_type = std::conditional_t&lt;    std::is_void_v&lt;T&gt;,    void_promise,    value_promise&lt;T&gt;&gt;;\u0421\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435:Task&lt;void&gt;              -&gt; void_promiseTask&lt;T&gt;, T != void      -&gt; value_promise&lt;T&gt;\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f Task&lt;void&gt; \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 promise type \u0441 return_void(), \u0430 \u0434\u043b\u044f Task&lt;T&gt; \u043f\u0440\u0438 T != void \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 promise type \u0441 return_value(&#8230;).base_promisevoid_promise \u0438 value_promise&lt;T&gt; \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 co_return.\u041e\u0431\u0449\u0438\u043c\u0438 \u0434\u043b\u044f \u043d\u0438\u0445 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f get_return_object(), initial_suspend(), final_suspend(), unhandled_exception() \u0438 \u043f\u043e\u043b\u0435 exception.\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u0430 \u0432 base_promise&lt;Promise&gt;. \u042d\u0442\u043e \u0443\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 promise object, \u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 promise types: void_promise \u0438 value_promise&lt;T&gt;.base_promise&lt;Promise&gt; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0438\u043f promise \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 Promise, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e std::coroutine_handle&lt;Promise&gt;::from_promise(&#8230;) \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 promise object [1][3].\u0412 get_return_object() \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 static_cast&lt;Promise&amp;&gt;(*this), \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f coroutine handle \u0447\u0435\u0440\u0435\u0437&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-478643","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/478643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=478643"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/478643\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=478643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=478643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=478643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}