std::move_only_function

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Function objects
Function wrappers
(C++11)
move_only_function
(C++23)
(C++11)
Partial function application
(C++20)(C++23)
(C++11)
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Reference wrappers
(C++11)(C++11)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Negators
(C++17)
Searchers
Constrained comparators
Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
 
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...)>;
template< class R, class... Args >
class move_only_function<R(Args...) noexcept>;
template< class R, class... Args >
class move_only_function<R(Args...) &>;
template< class R, class... Args >
class move_only_function<R(Args...) & noexcept>;
template< class R, class... Args >
class move_only_function<R(Args...) &&>;
template< class R, class... Args >
class move_only_function<R(Args...) && noexcept>;
template< class R, class... Args >
class move_only_function<R(Args...) const>;
template< class R, class... Args >
class move_only_function<R(Args...) const noexcept>;
template< class R, class... Args >
class move_only_function<R(Args...) const &>;
template< class R, class... Args >
class move_only_function<R(Args...) const & noexcept>;
template< class R, class... Args >
class move_only_function<R(Args...) const &&>;
template< class R, class... Args >

class move_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) Callable target — functions, lambda expressions, bind expressions, 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 std::function, invoking an empty std::move_only_function results in undefined behavior.

std::move_only_functions supports every possible combination of cv-qualifiers, ref-qualifiers, and noexcept-specifiers not including volatile 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)
(C++23)
replaces or destroys the target
(public member function)
(C++23)
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

overloads the std::swap algorithm
(function)
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 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 std::function Notes.

Feature-test macro Value Std Comment
__cpp_lib_move_only_function 202110L (C++23) std::move_only_function

Example

#include <iostream>
#include <functional>
#include <future>
 
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)
wraps callable object of any copy constructible type with specified function call signature
(class template)