- coroutine[meta header]
- std[meta namespace]
- class[meta id-type]
- cpp20[meta cpp]
namespace std {
struct suspend_always {
constexpr bool await_ready() const noexcept { return false; }
constexpr void await_suspend(coroutine_handle<>) const noexcept {}
constexpr void await_resume() const noexcept {}
};
}
- coroutine_handle<>[link coroutine_handle.md]
コルーチンを中断させる、トリビアルAwaitable型。
名前 | 説明 | 対応バージョン |
---|---|---|
await_ready |
コルーチンを常に中断する | C++20 |
await_suspend |
コルーチン中断時に何もしない | C++20 |
await_resume |
コルーチン再開時に何もしない | C++20 |
#include <coroutine>
#include <iostream>
#include <utility>
struct task {
struct promise_type {
int value_;
auto get_return_object() { return task{*this}; }
auto initial_suspend()
{
return std::suspend_always{};
}
auto final_suspend() noexcept
{
return std::suspend_always{};
}
void return_value(int x) { value_ = x; }
void unhandled_exception() { std::terminate(); }
};
using coro_handle = std::coroutine_handle<promise_type>;
~task()
{
if (coro_)
coro_.destroy();
}
task(task const&) = delete;
task(task&& rhs)
: coro_(std::exchange(rhs.coro_, nullptr)) {}
int get()
{
if (!coro_.done()) {
coro_.resume();
}
return coro_.promise().value_;
}
private:
explicit task(promise_type& p)
: coro_(coro_handle::from_promise(p)) {}
coro_handle coro_;
};
task f()
{
std::cout << "coroutine" << std::endl;
co_return 42;
}
int main()
{
auto c = f();
std::cout << "main" << std::endl;
int r = c.get();
std::cout << "result=" << r << std::endl;
}
- std::suspend_always[color ff0000]
- std::coroutine_handle[link coroutine_handle.md]
- resume()[link coroutine_handle/resume.md]
- done()[link coroutine_handle/done.md]
- from_promise[link coroutine_handle/from_promise.md]
- promise()[link coroutine_handle/promise.md]
- std::exchange[link /reference/utility/exchange.md]
main
coroutine
result=42
- C++20
- Clang: ??
- GCC: 11.1
- Visual C++: ??