std::move_o...::operator()(3)
π NAME π
okay bestie, we're in the C++ Standard Libary manual, and this page is std::move_o...::operator()(3) β yes, that ellipsis is literally in the page title, no cap.
std::move_only_function::operator() β std::move_only_function::operator().
It's the call operator, and the NAME section just says it twice. Iconic. π
β¨ Synopsis β¨
R operator()( Args... args ) /*cv*/ /*ref*/ noexcept(/*noex*/);
(since C++23)
It invokes the stored callable target with the parameters args. The /*cv*/, /*ref*/, and /*noex*/ parts of operator() are identical to those of the template parameter of std::move_only_function.
Equivalent to:
return std::invoke_r<R>(/*cv-ref-cast*/(f), std::forward<Args>(args)...);
where f is a cv-unqualified lvalue that denotes the target object of *this, and /*cv-ref-cast*/(f) is equivalent to:
* fif cv ref is either empty or&, orstd::as_const(f)if cv ref is eitherconstorconst &, orstd::move(f)if cv ref is&&, orstd::move(std::as_const(f))if cv ref isconst &&.
The behavior is undefined if *this is empty. Bestie, do not call an empty one β that's UB and it's giving "we don't do that here" ππ
πΈ Parameters πΈ
argsβ parameters to pass to the stored callable target.
π Return value π
std::invoke_r<R>(/*cv-ref-cast*/(f), std::forward<Args>(args)...).
π Exceptions π
Propagates the exception thrown by the underlying function call.
π¦ Example π¦
The following example shows how std::move_only_function can passed to other functions by value. Also, it shows how std::move_only_function can store lambdas.
The page literally says // Run this code right before the snippet:
#include <iostream>
#include <functional>
void call(std::move_only_function<int() const> f) // can be passed by value
{
std::cout << f() << '\n';
}
int normal_function()
{
return 42;
}
int main()
{
int n = 1;
auto lambda = [&n](){ return n; };
std::move_only_function<int() const> f = lambda;
call(std::move(f));
n = 2;
call(lambda);
f = normal_function;
call(std::move(f));
}
π Output π
1
2
42
π See also π
operator()invokes the target (public member function ofstd::function<R(Args...)>)operator()calls the stored function (public member function ofstd::reference_wrapper<T>)invokeβ invokes any Callable object with given argumentsinvoke_rβ and possibility to specify return type- The page also has these version/type tags:
(C++17),(since C++23),(C++23),(function template)
Source/version: http://cppreference.com, date 2024.06.10, page std::move_o...::operator()(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