CKTIS / C++
std::suspend_never — awaiting without suspending a coroutine
std::suspend_never from <coroutine> is a trivial awaiter that does not suspend a coroutine and produces no value. It helps specify immediate coroutine startup or completion; at completion, careful attention to coroutine-state lifetime is necessary.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <coroutine>
co_await std::suspend_never{};std::suspend_never is a type from <coroutine> whose objects can directly act as awaiters for co_await. It does not suspend the coroutine and produces no value: the result type is void. In contrast, std::suspend_always, when used directly as an awaiter, always suspends the coroutine.
await_ready() returns true, so the await protocol skips await_suspend(std::coroutine_handle<>) and calls await_resume(). Both of the latter functions do nothing and return void; all three are constexpr, const, and noexcept. This prevents suspension at this particular point, not throughout the entire coroutine. A custom promise await_transform can change how the co_await operand is interpreted.
The implicit co_await expressions for initial_suspend() and final_suspend() skip await_transform, but can still select an overloaded operator co_await that supplies a suspending awaiter. The return type alone therefore does not guarantee that the coroutine will not suspend. If the std::suspend_never object returned by initial_suspend() is itself used as the awaiter, execution proceeds straight into the coroutine body. If the object returned by final_suspend() is itself used as the awaiter, there is no final suspension and the coroutine state is destroyed on completion. Any retained handle to that state becomes dangling—you must not subsequently call resume(), done(), or destroy() through it.
In the example, immediate_task stores no handle. run() performs both prints before returning to main(), and its state is cleaned up automatically. The entry targets C++23; the cited site hosts an evolving standard draft, not a frozen C++23 publication. Execution in a production runtime has not been attested.
Examples
#include <coroutine>
#include <exception>
#include <iostream>
struct immediate_task {
struct promise_type {
immediate_task get_return_object() noexcept { return {}; }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() noexcept {}
void unhandled_exception() noexcept { std::terminate(); }
};
};
immediate_task run() {
std::cout << "before\n";
co_await std::suspend_never{};
std::cout << "after\n";
co_return;
}
int main() {
run();
std::cout << "returned\n";
}
Result
before after returned
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.