std::move_only_function:: operator()
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
R operator
(
)
(
Args...
args
)
/*cv*/
/*ref*/
noexcept
(
/*noex*/
)
;
|
(自 C++23 起) | |
使用参数
args
调用存储的可调用目标。
/*cv*/
、
/*ref*/
和
/*noex*/
部分与
std::move_only_function
模板参数的对应部分完全一致。
等价于
return
std::
invoke_r
<
R
>
(
/*cv-ref-cast*/
(
f
)
,
std::
forward
<
Args
>
(
args
)
...
)
;
,其中
f
是表示
*
this
目标对象的无 cv 限定左值,而
/*cv-ref-cast*/
(
f
)
等价于:
- f 若 cv ref 为空或为 & ,或
- std:: as_const ( f ) 若 cv ref 为 const 或 const & ,或
- std :: move ( f ) 若 cv ref 为 && ,或
- std :: move ( std:: as_const ( f ) ) 若 cv ref 为 const && 。
若 * this 为空,则行为未定义。
目录 |
参数
| args | - | 传递给存储的可调用目标的参数 |
返回值
std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) 。
异常
传播由底层函数调用抛出的异常。
示例
以下示例展示了如何通过值传递 std::move_only_function 给其他函数,同时也展示了 std::move_only_function 如何存储 lambda 表达式。
#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)); }
输出:
1 2 42
参见
|
调用目标函数
(std::function<R(Args...)> 的公开成员函数) |
|
|
调用存储的函数
(std::reference_wrapper<T> 的公开成员函数) |
|
|
(C++17)
(C++23)
|
以给定参数调用任意
可调用
对象
并可指定返回类型
(C++23 起)
(函数模板) |