CKTIS / C++
std::move_only_function — a move-only callable wrapper
C++23 std::move_only_function stores a callable with a specified signature, including a lambda owning an exclusive resource. The wrapper can be moved but not copied. It must contain a target before it is called.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <functional>
std::move_only_function<R(Args...)> f;
std::move_only_function<R(Args...) const> cf;
std::move_only_function<R(Args...) &> lf;
std::move_only_function<R(Args...) && noexcept> rf;std::move_only_function, declared in <functional> and available since C++23, is a type-erased wrapper: it exposes a fixed call signature while hiding the concrete stored type. Unlike std::function, it does not require a copyable target; it can hold a lambda capturing a std::unique_ptr. The wrapper itself cannot be copied.
The signature type can include const, a ref-qualifier & or &&, and noexcept. These control how the wrapper may be called and constrain the target. For example, std::move_only_function<int() && noexcept> requires an rvalue wrapper and a target callable without throwing. The && qualifier alone does not guarantee one-shot use.
A default-constructed wrapper is empty. Check if (f) before calling when target presence is uncertain: calling an empty wrapper in C++23 causes undefined behavior, not a guaranteed std::bad_function_call exception. Assigning nullptr removes the target. After a move, the source remains valid but has an unspecified value; do not assume it is empty.
The example moves a wrapper containing a resource-owning lambda, invokes the target, and then explicitly removes it. C++23 support is required in the standard library too; a compiler flag alone does not establish feature availability or production-environment readiness. The supplied source is an evolving working draft, not a frozen C++23 text.
Examples
#include <functional>
#include <iostream>
#include <memory>
#include <utility>
int main() {
std::move_only_function<int(int)> add =
[base = std::make_unique<int>(40)](int x) {
return *base + x;
};
auto task = std::move(add);
if (task) {
std::cout << task(2) << '\n';
}
task = nullptr;
std::cout << std::boolalpha << static_cast<bool>(task) << '\n';
}
Result
42 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.