std::move_only_function
Defined in header <functional>
|
||
template
<
class... >
class move_only_function; // not defined |
(1) | (since C++23) |
template
<
class R, class... Args
>
class move_only_function<R(Args...)
>
;
|
(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) Callable
target — functions, lambda expressions, bind expressions
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 std::function, invoking an empty
std::move_only_function
std::move_only_function
s supports every possible combination of cv-qualifiers (not including volatile), ref-qualifiers, and noexcept-specifiers provided in its template parameter. These qualifiers and specifier (if any) are added to its operator()
std::move_only_function
satisfies the requirements of MoveConstructible and MoveAssignable, but does not satisfy CopyConstructible or CopyAssignable
Member types
Type | Definition |
result_type
|
R
|
Member 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
(C++23)
|
specializes the std::swap algorithm (function) |
(C++23)
|
compares a std::move_only_function with nullptr (function) |
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 std::reference_wrapper 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
std::function
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_move_only_function |
202110L |
(C++23) | std::move_only_function
|
Example
#include <functional> #include <future> #include <iostream> int main() { std::packaged_task<double()> packaged_task([](){ return 3.14159; }); std::future<double> future = packaged_task.get_future(); auto lambda = [task = std::move(packaged_task)]() mutable { task(); }; // std::function<void()> function = std::move(lambda); // Error std::move_only_function<void()> function = std::move(lambda); // OK function(); 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) |