【发布时间】:2021-07-22 04:05:27
【问题描述】:
VS2019最新c++编译器。
错误是:“协程的承诺必须声明'return_value'或'return_void'”
示例来自 David Mazières https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html 博客 在不同的编译器 GCC 10.2 下工作。
我在VS2019中无法获取源代码编译。
#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>
struct ReturnObject {
struct promise_type {
ReturnObject get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return{}; }
void unhandled_exception() {}
};
};
struct Awaiter {
std::coroutine_handle<>* hp_;
constexpr bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<> h) { *hp_ = h; }
constexpr void await_resume() const noexcept {}
};
ReturnObject
counter(std::coroutine_handle<>* continuation_out)
{
Awaiter a{ continuation_out };
for (unsigned i = 0;; ++i) {
co_await a;
std::cout << "counter: " << i << std::endl;
}
}
void
main1()
{
std::coroutine_handle<> h;
counter(&h);
for (int i = 0; i < 100; ++i) {
std::cout << "In main1 function\n";
h();
}
h.destroy();
}
我错过了什么? C++ 中的协程新手。谁不是?
【问题讨论】:
标签: c++ visual-studio-2019 c++20 c++-coroutine