std::move_only_function(3)
hey bestie π welcome to the std::move_only_function page from the C++ Standard Libary manual β yes, that's the manual name, and we're keeping it exactly as it appears π«Ά
π NAME π
std::move_only_function β std::move_only_function (yes, literally the same thing twice, and honestly? iconic π
)
π¦ Synopsis π¦
Defined in header <functional>.
okay so here's the template buffet, bestie β every single one, no skipping π«Ά
template< class... > (1) (since C++23)
class move_only_function; // not defined
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>; (2) (since C++23)
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>;
okay so what does it all mean? 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 β like, for real undefined behavior, not a cute little error message π
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. periodt π
πΈ Member types πΈ
here's the one member type, and she's simple:
- Type:
result_typeβ Definition:Rβ
π Member functions π
the member functions, all C++23, all public member functions:
constructorβ constructs a newstd::move_only_functionobject (C++23) (public member function) π«Άdestructorβ destroys astd::move_only_functionobject (C++23) (public member function) π«Άoperator=β replaces or destroys the target (C++23) (public member function)swapβ swaps the targets of twostd::move_only_functionobjects (C++23) (public member function)operator boolβ checks if thestd::move_only_functionhas a target (C++23) (public member function)operator()β invokes the target (C++23) (public member function)
β¨ Non-member functions β¨
and the non-member squad:
swap(std::move_only_function)β overloads thestd::swapalgorithm (C++23) (function)operator==β compares astd::move_only_functionwithnullptr(C++23) (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. β οΈ
and the feature-test macro is giving:
- Feature-test macro:
__cpp_lib_move_only_function- Value:
202110L - Std:
(C++23) - Feature:
std::move_only_functionπ
- Value:
π Example π
run this code, bestie:
// Run this code
#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 π
functionβ wraps callable object of any copy constructible type with specified function call signature (C++11) (class template) π
that's the whole page, bestie π«Ά source: http://cppreference.com, date: 2024.06.10, manual page: std::move_only_function(3) β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms