Cppcoro 不接受来自 constom 可等待结构的结果

Cppcoro not accepting result from an constom awaitable struct

我决定编写自己的 awaitable 以加载程序以了解 C++ 协程的工作原理。现在,我想构建我自己的结构,它等同于:

cppcoro::task<int> bar()
{
    co_yield 42;
}

这是我看完CppReference's coroutine page后想到的。其中指出 最后, awaiter.await_resume() 被调用,其结果是整个 co_await expr 表达式的结果。 我假设改变 return 类型的 await_resume() 足以让 barmake_awaitable 具有相同的功能。

#include <iostream>
#include <coroutine>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>

auto make_awaitable() {
    using return_type = int;
    struct awaitable {
        bool await_ready() {return false;}
        void await_suspend(std::coroutine_handle<> h) {
            std::cout << "In await_suspend()" << std::endl;
            h.resume();
        }
        int await_resume() {return 42;};
        
    };
    return awaitable{};
}

cppcoro::task<int> foo()
{
    int n = co_await make_awaitable();
    std::cout << n << std::endl;
}

int main()
{
    cppcoro::sync_wait(foo());
    std::cout << "Called coro" << std::endl;
    return 0;
}

但是 运行 代码生成断言错误。

coro_test: /usr/include/cppcoro/task.hpp:187: cppcoro::detail::task_promise<T>::rvalue_type cppcoro::detail::task_promise<T>::result() && [with T = int; cppcoro::detail::task_promise<T>::rvalue_type = int]: Assertion `m_resultType == result_type::value' failed.

我做错了什么?

编译器:GCC 10

问题是,我认为,你将你的 return 类型声明为 task<int> 但你实际上 co_return 没有任何 int.

如果您 co_return n,问题会消失吗?