Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::noop_coroutine — a handle to a no-op coroutine

std::noop_coroutine() returns a non-null handle to a special coroutine whose resumption and destruction have no observable effects. It is not an empty handle.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <coroutine>
std::noop_coroutine_handle std::noop_coroutine() noexcept;

std::noop_coroutine() from <coroutine> is a noexcept function. It returns std::noop_coroutine_handle, an alias for std::coroutine_handle<std::noop_coroutine_promise>. It requires neither arguments nor a user-written coroutine function.

The returned handle is not empty: conversion to bool yields true, address() is non-null, and done() yields false. Calling resume(), operator(), or destroy() has no effects; in particular, destroy() does not invalidate this handle. These guarantees do not apply to arbitrary coroutine handles.

The handle can be converted to std::coroutine_handle<>. Resumption and destruction still have no observable effects after this conversion. Do not assume that handles obtained from two separate calls to std::noop_coroutine() compare equal: the standard permits either comparison result.

The example checks the state before and after the operations, including conversion to std::coroutine_handle<>. It prints no addresses and does not compare separate invocations, keeping the expected output deterministic.

Examples

#include <coroutine>
#include <iostream>

int main() {
    auto h = std::noop_coroutine();
    std::cout << std::boolalpha;
    std::cout << "valid=" << static_cast<bool>(h)
              << " done=" << h.done() << '\n';

    h.resume();
    h();
    h.destroy();

    std::coroutine_handle<> erased = h;
    erased.resume();
    erased.destroy();

    std::cout << "after: valid=" << static_cast<bool>(h)
              << " done=" << h.done() << '\n';
}

Result

valid=true done=false
after: valid=true done=false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources