为什么 co_await 不能返回字符串?

xml*_*lmx 7 c++ gcc compiler-errors coroutine c++20

#include <coroutine>
#include <string>

template<typename T>
struct Awaiter final {
    bool await_ready() const { return false; }    
    void await_suspend(std::coroutine_handle<>) const {}    
    T await_resume() const { return T{}; }
};

struct ReturnObject {
  struct promise_type {
    ReturnObject get_return_object() { return {}; }
    std::suspend_never initial_suspend() 
        noexcept { return {}; }
    std::suspend_never final_suspend() 
        noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() {}
  };
};

ReturnObject f()
{
    auto a1 = Awaiter<int>{};
    [[maybe_unused]] auto v1 = co_await a1; // ok    
    auto a2 = Awaiter<std::string>{};
    [[maybe_unused]] auto v2 = co_await a2; // error
}

int main() { f(); }
Run Code Online (Sandbox Code Playgroud)

请参阅:在线演示

错误信息:

error: no suspend point info for ''co_await' not supported 
       by dump_decl<declaration error>'
   37 |     [[maybe_unused]] auto v2 = co_await a2; // error
      |                                ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

为什么 co_await 不能返回字符串?

Jod*_*cus 6

这是 GCC 中协程实现中的编译器错误,因为标准的当前草案中没有任何内容禁止自定义/复合类型await_resume(如用任何用户定义类型替换 string 时可以看到的那样)。例如,相同的代码使用该标志使用最新版本的 MSVC进行编译/std:c++latest(这并不奇怪,因为主要在草案上进行开发的 Gor Nishanov 使用 Visual Studio 来实现原型,这可能是当时经过测试最多的实现写这个答案)。