写一个cplusplus20协程最简单的例子
时间: 2023-02-20 10:25:01 浏览: 123
C++20 in Examples
一个最简单的C++20协程例子是:
#include <iostream>
#include <experimental/coroutine>
using namespace std;
struct MyCoroutine {
struct promise_type {
auto get_return_object() {
return MyCoroutine{};
}
auto initial_suspend() {
return std::experimental::suspend_always{};
}
auto final_suspend() {
return std::experimental::suspend_always{};
}
void return_void() {}
void unhandled_exception() {}
};
};
MyCoroutine f() {
cout << "Hello from coroutine!" << endl;
co_return;
}
int main() {
f();
return 0;
}
阅读全文