Coroutine Machinery

To write your own coroutine return type, such as a generator, you provide a promise type and drive the coroutine through a handle. This page explains both, then combines them into a working generator.

The Promise Type

Every coroutine has an associated promise type. This type acts as a controller for the coroutine, defining how it behaves at key points in its lifecycle. The promise type is not something you pass to the coroutine.

The compiler expects to find a type named promise_type nested inside your coroutine’s return type. If your coroutine returns Generator<int>, the compiler looks for Generator<int>::promise_type. Formally, the compiler uses std::coroutine_traits<R, Args…​>::promise_type, where R is the return type and Args are the parameter types. For a non-static member function, the type of the implicit object parameter comes first in Args. The primary template forwards to R::promise_type, and a specialization can choose a different promise type.

Required Methods

The promise type provides these methods:

get_return_object()

Called to create the object returned to the caller of the coroutine.

initial_suspend()

Called immediately after get_return_object(). Returns an awaiter that determines whether the coroutine should suspend before running any of its body. Return std::suspend_never{} to start executing immediately, or std::suspend_always{} to suspend before the first statement.

final_suspend()

Called when the coroutine completes (either normally or via exception). Returns an awaiter that determines whether to suspend one last time or destroy the coroutine state immediately. The expression co_await promise.final_suspend() must not be potentially-throwing, so declare final_suspend() and the members of the awaiter it returns noexcept.

return_void() or return_value(v)

co_return v; with a non-void operand calls return_value(v). co_return; and falling off the end of the body call return_void(). Provide the one your coroutine uses. A promise type that declares both is ill-formed.

unhandled_exception()

Called if an exception escapes the coroutine body. Typically you either rethrow the exception, store it for later, or terminate the program.

The Compiler Transformation

The compiler transforms your coroutine body into something resembling this pseudocode:

The co_await keywords below are intentional. This follows the C++ standard’s own description (C++20 [dcl.fct.def.coroutine]/5), which uses co_await to express the logical suspension points. The standard specifies the get_return_object() call separately and sequences it before initial_suspend(). The compiler expands each co_await into the full awaiter protocol (await_ready, await_suspend, await_resume) as described in Awaitables and Awaiters.
What the compiler generates
{
    promise_type promise;
    auto return_object = promise.get_return_object();

    try {
        co_await promise.initial_suspend();  (1)
        // your coroutine body goes here
    }
    catch (...) {
        if (!initial_await_resume_called)  (2)
            throw;
        promise.unhandled_exception();
    }

final_suspend:  (3)
    co_await promise.final_suspend();  (4)
}
// coroutine frame is destroyed when control flows off the end
1 Suspension point before the body runs. If initial_suspend() returns suspend_always, the coroutine starts suspended.
2 This flag becomes true just before the initial awaiter’s await_resume() runs. An exception thrown earlier propagates to the caller instead of reaching unhandled_exception().
3 Every co_return statement jumps to this label after calling return_void() or return_value().
4 Suspension point after the body completes. If final_suspend() returns suspend_always, the frame persists for the caller to inspect or destroy.

Important observations:

  • The return object is created before initial_suspend() runs, so it is available even if the coroutine suspends immediately

  • final_suspend() determines whether the coroutine frame persists after completion. If it returns suspend_always, you must manually destroy the coroutine; if it returns suspend_never, the frame is destroyed automatically

Tracing Promise Behavior

#include <coroutine>
#include <iostream>

struct TracePromise
{
    struct promise_type
    {
        promise_type()
        {
            std::cout << "promise constructed" << std::endl;
        }

        ~promise_type()
        {
            std::cout << "promise destroyed" << std::endl;
        }

        TracePromise get_return_object()
        {
            std::cout << "get_return_object called" << std::endl;
            return TracePromise{
                std::coroutine_handle<promise_type>::from_promise(*this)};
        }

        std::suspend_never initial_suspend()
        {
            std::cout << "initial_suspend called" << std::endl;
            return {};
        }

        std::suspend_always final_suspend() noexcept
        {
            std::cout << "final_suspend called" << std::endl;
            return {};
        }

        void return_void()
        {
            std::cout << "return_void called" << std::endl;
        }

        void unhandled_exception()
        {
            std::cout << "unhandled_exception called" << std::endl;
        }
    };

    std::coroutine_handle<promise_type> handle;

    explicit TracePromise(std::coroutine_handle<promise_type> h)
        : handle(h)
    {
    }

    // final_suspend() returns suspend_always, so the frame stays alive
    // until the handle is destroyed explicitly.
    ~TracePromise()
    {
        std::cout << "destroying coroutine handle" << std::endl;
        handle.destroy();
    }
};

TracePromise trace_coroutine()
{
    std::cout << "coroutine body begins" << std::endl;
    co_return;
}

int main()
{
    std::cout << "calling coroutine" << std::endl;
    [[maybe_unused]] auto result = trace_coroutine();
    std::cout << "coroutine returned" << std::endl;
}

Output:

calling coroutine
promise constructed
get_return_object called
initial_suspend called
coroutine body begins
return_void called
final_suspend called
coroutine returned
destroying coroutine handle
promise destroyed

The promise is constructed first, then get_return_object() creates the return value, then initial_suspend() runs. Since initial_suspend() returns suspend_never, the coroutine body executes immediately. Since final_suspend() returns suspend_always, the coroutine suspends one last time, and the promise is not destroyed until the coroutine handle is explicitly destroyed.

A coroutine may fall off the end of its body without executing co_return. If its promise type lacks a return_void() method, the behavior is undefined. Your promise type must therefore have return_void() whenever any code path might reach the end of the coroutine body without an explicit co_return.

Coroutine Handle

A std::coroutine_handle<> is a lightweight object that refers to a coroutine. It is similar to a pointer: it does not own the memory it references, and copying it does not copy the coroutine.

Basic Operations

  • handle() or handle.resume(): Resume the suspended coroutine

  • handle.done(): Returns true if the suspended coroutine is at its final suspend point

  • handle.destroy(): Destroy the coroutine frame (frees memory)

  • handle.promise(): Returns a reference to the promise object (typed handles only)

Typed vs Untyped Handles

std::coroutine_handle<>

The most basic form (equivalent to std::coroutine_handle<void>). Can reference any coroutine but provides no access to the promise object.

std::coroutine_handle<PromiseType>

A typed handle that knows about a particular promise type. Can be converted to the void handle. Provides a promise() method that returns a reference to the promise object.

Creating Handles from Promises

Inside get_return_object(), you can obtain the coroutine handle using:

std::coroutine_handle<promise_type>::from_promise(*this)

Since get_return_object() is called on the promise object (as this), this method returns a handle to the coroutine containing that promise.

Putting It Together: Building a Generator

A generator is a function that produces a sequence of values on demand. Instead of computing all values upfront, a generator computes each value when requested using co_yield.

How co_yield Works

The expression co_yield value is transformed by the compiler into:

What the compiler generates
co_await promise.yield_value(value)

The yield_value method receives the yielded value, stores it somewhere accessible, and returns an awaiter (usually std::suspend_always) to suspend the coroutine.

Complete Generator Implementation

#include <coroutine>
#include <exception>
#include <iostream>

struct Generator
{
    struct promise_type
    {
        int current_value;

        Generator get_return_object()
        {
            return Generator{
                std::coroutine_handle<promise_type>::from_promise(*this)
            };
        }

        std::suspend_always initial_suspend() { return {}; }
        std::suspend_always final_suspend() noexcept { return {}; }

        std::suspend_always yield_value(int value)
        {
            current_value = value;
            return {};
        }

        void return_void() {}
        void unhandled_exception() { std::terminate(); }
    };

    std::coroutine_handle<promise_type> handle;

    Generator(std::coroutine_handle<promise_type> h) : handle(h) {}

    ~Generator()
    {
        if (handle)
            handle.destroy();
    }

    // Disable copying
    Generator(Generator const&) = delete;
    Generator& operator=(Generator const&) = delete;

    // Enable moving
    Generator(Generator&& other) noexcept
        : handle(other.handle)
    {
        other.handle = nullptr;
    }

    Generator& operator=(Generator&& other) noexcept
    {
        if (this != &other)
        {
            if (handle)
                handle.destroy();
            handle = other.handle;
            other.handle = nullptr;
        }
        return *this;
    }

    bool next()
    {
        if (!handle || handle.done())
            return false;
        handle.resume();
        return !handle.done();
    }

    int value() const
    {
        return handle.promise().current_value;
    }
};

Generator count_to(int n)
{
    for (int i = 1; i <= n; ++i)
    {
        co_yield i;
    }
}

int main()
{
    auto gen = count_to(5);

    while (gen.next())
    {
        std::cout << gen.value() << std::endl;
    }
}

Output:

1
2
3
4
5

Key Design Decisions

initial_suspend() returns suspend_always

The coroutine suspends before running any user code. This means the first call to next() starts the coroutine running.

final_suspend() returns suspend_always

The coroutine frame persists after completion. This is necessary because next() checks handle.done() after the last value.

Generator owns the handle

The destructor calls handle.destroy() to free the coroutine frame. Copying is disabled to avoid double-free; moving transfers ownership.

yield_value stores and suspends

Stores the yielded value in current_value and returns suspend_always to pause the coroutine after each yield.

Fibonacci Generator

Here is a more interesting generator that produces the Fibonacci sequence. It reuses the Generator type from the previous example, so the listing shows only the new coroutine and main:

Generator fibonacci()
{
    int a = 0, b = 1;
    while (true)
    {
        co_yield a;
        int next = a + b;
        a = b;
        b = next;
    }
}

int main()
{
    auto fib = fibonacci();

    for (int i = 0; i < 10 && fib.next(); ++i)
    {
        std::cout << fib.value() << " ";
    }
    std::cout << std::endl;
}

Output:

0 1 1 2 3 5 8 13 21 34

The Fibonacci generator runs an infinite loop internally. But because it yields and suspends after each value, the caller controls when (and whether) to ask for more values.

The variables a and b persist across yields because they live in the coroutine frame, typically on the heap.