Template Class Task

Nested Relationships

Nested Types

Class Documentation

template<typename Result = void>
class Task

Lightweight C++20 coroutine for simple async operations.

Unlike Job<T>, Task does not use the work-stealing scheduler. Tasks are executed directly when co_awaited, making them suitable for simple sequential async code.

Example:

Task<int> simple_async_work()
{
    co_return 42;
}

Task<void> caller()
{
    int value = co_await simple_async_work();
    co_return;
}

Template Parameters:

Result – Return type (use void for tasks without return values)

Public Types

using promise_type = Promise

Public Functions

Task() = default
inline Task(Task &&other) noexcept
inline Task &operator=(Task &&other) noexcept
inline ~Task()
Task(const Task&) = delete
Task &operator=(const Task&) = delete
inline auto operator co_await() noexcept
struct Awaiter

Public Functions

inline bool await_ready() noexcept
inline auto await_suspend(std::coroutine_handle<> calling) noexcept
template<typename T = Result>
inline void await_resume() noexcept
template<typename T = Result>
inline T await_resume() noexcept

Public Members

std::coroutine_handle<Promise> handle
struct FinalAwaiter

Public Functions

inline bool await_ready() noexcept
template<typename P>
inline auto await_suspend(std::coroutine_handle<P> handle) noexcept
inline void await_resume() noexcept
struct Promise

Public Functions

inline Task get_return_object()
inline void unhandled_exception() noexcept
inline void return_value(Result res) noexcept
inline std::suspend_always initial_suspend() noexcept
inline FinalAwaiter final_suspend() noexcept
inline Task<void> get_return_object()
inline void unhandled_exception() noexcept
inline void return_void() noexcept
inline std::suspend_always initial_suspend() noexcept
inline Task<void>::FinalAwaiter final_suspend() noexcept

Public Members

std::coroutine_handle continuation
Result result