C++ Reference ยท Memory and Utilities
C++ std::move_only_function (C++23)
Reference for C++23 std::move_only_function: a callable wrapper for move-only targets such as lambdas that capture unique_ptr, with qualifiers and noexcept.
std::move_only_function, added in C++23 and declared in <functional>, is like std::function but accepts move-only callables. Its main benefit: a lambda that captures a std::unique_ptr, a std::promise or a std::packaged_task cannot be stored in std::function, but fits in a move_only_function.
The wrapper itself is movable, not copyable. It also lets you put const, &, && and noexcept qualifiers into the signature, so the call operator is exactly as constrained as you declare. It works with GCC 15 in C++23 mode.
std::move_only_function<int()> job = [p = std::make_unique<int>(7)] { return *p; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class... Sig> class move_only_function; // Sig is R(Args...) cv ref noexcept move_only_function() noexcept; move_only_function(nullptr_t) noexcept; move_only_function(move_only_function&&) noexcept; template<class F> move_only_function(F&& f); R operator()(Args... args) cv ref noexcept(noex); // qualifiers copied from the signature explicit operator bool() const noexcept;
| Part | Description |
|---|---|
R(Args...) |
Return and parameter types of the call. |
const |
The target is invoked as a const object; non-const targets are rejected. |
&, && |
The wrapper must be called on an lvalue or rvalue respectively. |
noexcept |
The call operator is noexcept; the target must not throw (otherwise terminate). |
Returns: the target's result converted to R. Errors / undefined behavior: calling an empty move_only_function is undefined behavior; it does not throw bad_function_call. Test if (f) first.
Usage notes
- No copying: copy construction and assignment are deleted. Pass it by value with
std::move, or store it in a movable container such asstd::vectororstd::queue. - No
target()ortarget_type(): unlikestd::function, it has no RTTI access to the stored object. - Const correctness is real:
std::function::operator()isconsteven when the target mutates. Here, a plainR(Args...)signature calls the target as a non-const object, and declaringR(Args...) constforbids mutable targets. - Small buffer: a small target is stored inline, larger ones on the heap; the threshold is implementation-defined.
- Good fit: thread pools, task queues, completion callbacks that hand over ownership of a buffer or a promise.
- Not a reference wrapper: if you only need to call something during a function and not keep it, accept a template parameter or a reference; both avoid allocation.
- Library support: in C++23 mode of GCC 15 it compiles and runs on our runner.
- Treat a moved-from
move_only_functionas empty: test it before calling, or assign a new target.