C ++协程:实现task <void>

Mas*_*ler 6 c++ templates c++20 c++-coroutine

因此,在这里,我试图理解协程的新的复杂概念。为此,用Clang进行编译clang++ -std=c++17 -fcoroutines-ts -stdlib=libc++就可以了。

task<>协程类型是最有用的概念之一,在这里提到它,甚至有一些有趣的实现,它们由Gor Nishanovcppcoro库提供

好吧,在最简单的情况下尝试自己看起来不错。因此,目标是实施应如下所示的工作方式:

    {
        auto producer = []() -> task<int> {
            co_return 1;
        };

        auto t = producer();

        assert(!t.await_ready());
        assert(t.result() == 1);
        assert(t.await_ready());
    }
Run Code Online (Sandbox Code Playgroud)

模板类task<>本身变得非常简单:

#pragma once

#include <experimental/coroutine>
#include <optional>

namespace stdx = std::experimental;

template <typename T=void>
struct task 
{
    template<typename U>
    struct task_promise;

    using promise_type = task_promise<T>;
    using handle_type = stdx::coroutine_handle<promise_type>;

    mutable handle_type m_handle;

    task(handle_type handle)
        : m_handle(handle) 
    {}

    task(task&& other) noexcept 
        : m_handle(other.m_handle)
    { other.m_handle = nullptr; };

    bool await_ready() 
    { return m_handle.done(); }

    bool await_suspend(stdx::coroutine_handle<> handle) 
    {
        if (!m_handle.done()) {
            m_handle.resume();
        }

        return false;
    }

    auto await_resume() 
    { return result(); }

    T result() const 
    {     
        if (!m_handle.done())
            m_handle.resume();  

        if (m_handle.promise().m_exception)
            std::rethrow_exception(m_handle.promise().m_exception);

        return *m_handle.promise().m_value;
    }

    ~task() 
    {
        if (m_handle)
            m_handle.destroy();
    }

    template<typename U>
    struct task_promise 
    {
        std::optional<T>    m_value {};
        std::exception_ptr  m_exception = nullptr;

        auto initial_suspend() 
        { return stdx::suspend_always{}; }

        auto final_suspend() 
        { return stdx::suspend_always{}; }

        auto return_value(T t) 
        {
            m_value = t;
            return stdx::suspend_always{};
        }

        task<T> get_return_object()
        { return {handle_type::from_promise(*this)}; }

        void unhandled_exception() 
        {  m_exception = std::current_exception(); }

        void rethrow_if_unhandled_exception()
        {
            if (m_exception)
                std::rethrow_exception(std::move(m_exception));
        }
    };

};
Run Code Online (Sandbox Code Playgroud)

抱歉,实际上无法使较小的代码完整且可编译。无论如何,它都可以工作,但是仍然存在的情况task<void>,它的用法可能如下所示:

    {
        int result = 0;

        auto int_producer = []() -> task<int> {
            co_return 1;
        };

        auto awaiter = [&]() -> task<> { // here problems begin
            auto i1 = co_await int_producer();
            auto i2 = co_await int_producer();

            result = i1 + i2;
        };

        auto t = awaiter();

        assert(!t.await_ready());
        t.await_resume();
        assert(result == 2);
    }
Run Code Online (Sandbox Code Playgroud)

后者似乎根本不是问题,它看起来似乎task_promise<U>需要专门化void(可以是没有空值情况的非模板结构)。因此,我尝试了:

    template<>
    struct task_promise<void>
    {
        std::exception_ptr  m_exception;

        void return_void() noexcept  {}

        task<void> get_return_object() noexcept
        { return {handle_type::from_promise(*this)}; }

        void unhandled_exception() 
        { m_exception = std::current_exception(); }

        auto initial_suspend() 
        { return stdx::suspend_always{}; }

        auto final_suspend() 
        { return stdx::suspend_always{}; }
    };
Run Code Online (Sandbox Code Playgroud)

整洁而简单...它会导致段错误,而没有任何可读的stacktrace =(当task<>更改为任何非空模板时,效果很好task<char>

我的模板专业化有什么问题?还是我在那些协程中缺少一些棘手的概念?

对于任何想法将不胜感激。

Oli*_*liv 4

显然,通常的嫌疑人就是罪犯:专业化!来自标准本身[temp.expl.spec]/7

编写专业化时,请注意其位置;或者让它编译将是一个足以点燃其自焚的考验。

为了避免出现问题,让我们尽可能简单:task_promise可以是非模板,并且尽快声明成员专业化:

template<class T=void>
struct task{
  //...
  struct task_promise{
    //...
    };
  };

//member specialization declared before instantiation of task<void>;
template<>
struct task<void>::task_promise{
  //...
  };
Run Code Online (Sandbox Code Playgroud)