From cppreference.com
Defined in header
template<class...>classmove_only_function;// not defined (1) (since C++23)template<classR,class...Args>classmove_only_function<R(Args...)>;template<classR,class...Args>classmove_only_function<R(Args...)noexcept>;template<classR,class...Args>classmove_only_function<R(Args...)&>;template<classR,class...Args>classmove_only_function<R(Args...)&noexcept>;template<classR,class...Args>classmove_only_function<R(Args...)&&>;template<classR,class...Args>classmove_only_function<R(Args...)&&noexcept>;template<classR,class...Args>classmove_only_function<R(Args...)const>;template<classR,class...Args>classmove_only_function<R(Args...)constnoexcept>;template<classR,class...Args>classmove_only_function<R(Args...)const&>;template<classR,class...Args>classmove_only_function<R(Args...)const&noexcept>;template<classR,class...Args>classmove_only_function<R(Args...)const&&>;template<classR,class...Args>classmove_only_function<R(Args...)const&&noexcept>; (2) (since C++23)Class template std::move_only_function is a general-purpose polymorphic function wrapper. std::move_only_function objects can store and invoke any constructible (not required to be move constructible)
target — functions,
,
, or other function objects, as well as pointers to member functions and pointers to member objects.
The stored callable object is called the target of std::move_only_function. If a std::move_only_function contains no target, it is called empty. Unlike
, invoking an emptystd::move_only_function results in undefined behavior.
std::move_only_functions supports every possible combination of
(not including volatile),
, and
provided in its template parameter. These qualifiers and specifier (if any) are added to its
.
std::move_only_function satisfies the requirements of
and
, but does not satisfy
or
.
Member types
Type Definition result_typeRMember functions
constructs a new std::move_only_function object
(public member function)
destroys a std::move_only_function object
(public member function)
replaces or destroys the target
(public member function)
swaps the targets of two std::move_only_function objects
(public member function)
checks if the std::move_only_function has a target
(public member function)
invokes the target
(public member function)
Non-member functions
Notes
Implementations may store a callable object of small size within the std::move_only_function object. Such small object optimization is effectively required for function pointers and
specializations, and can only be applied to types T for which std::is_nothrow_move_constructible_v<T> is true.
If a std::move_only_function returning a reference is initialized from a function or function object returning a prvalue (including a lambda expression without a trailing-return-type), the program is ill-formed because binding the returned reference to a temporary object is forbidden. See also
Notes.
macroValueStdFeature
(C++23)std::move_only_functionExample
Run this code
#include<functional>#include<future>#include<iostream>intmain(){std::packaged_task<double()>packaged_task([](){return3.14159;});std::future<double>future=packaged_task.get_future();autolambda=[task=std::move(packaged_task)]()mutable{task();};// std::function<void()> function = std::move(lambda); // Errorstd::move_only_function<void()>function=std::move(lambda);// OKfunction();std::cout<<future.get();}Output:
3.14159 See also
(C++11)
copyable wrapper of any copy constructible callable object
(class template)
(C++26)
non-owning wrapper of any callable object
(class template)
(C++26)
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature
(class template)