std::function
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <functional> |
||
template< class > class function; /* 未定义 */ |
(自 C++11 起) | |
template< class R, class... Args > class function<R(Args...)>; |
(自 C++11 起) | |
类模板 std::function
是通用的多态函数包装器。std::function
的实例能存储、复制和调用任何 CopyConstructible Callable target —— 函数(通过指向它的指针)、lambda 表达式、bind 表达式,或其他函数对象,以及指向成员函数的指针和指向数据成员的指针。
存储的可调用对象被称为 std::function
的目标 (target)。若 std::function
不含目标,则它被称作空 (empty)。调用空 std::function
的目标将导致抛出 std::bad_function_call 异常。
std::function
满足 CopyConstructible 和 CopyAssignable 的要求。
目录 |
[编辑] 成员类型
类型 | 定义 |
result_type
|
R
|
argument_type (已弃用于 C++17)(移除于 C++20) |
T 若 sizeof...(Args)==1 且 T 是 Args... 中的首个也是唯一类型 |
first_argument_type (已弃用于 C++17)(移除于 C++20) |
T1 若 sizeof...(Args)==2 且 T1 是 Args... 中两个类型中的首个 |
second_argument_type (已弃用于 C++17)(移除于 C++20) |
T2 若 sizeof...(Args)==2 且 T2 是 Args... 中两个类型中的第二个 |
[编辑] 成员函数
构造新的 std::function 实例(公有成员函数) | |
销毁 std::function 实例(公有成员函数) | |
赋值新目标 (公有成员函数) | |
交换内容 (公有成员函数) | |
(移除于 C++17) |
赋值新目标 (公有成员函数) |
检查是否含有目标 (公有成员函数) | |
调用目标 (公有成员函数) | |
目标访问 | |
获得存储目标的 typeid (公有成员函数) | |
获得指向存储目标的指针 (公有成员函数) |
[编辑] 非成员函数
(C++11) |
特化 std::swap 算法 (函数模板) |
(移除于 C++20) |
将 std::function 与 nullptr 比较 (函数模板) |
[编辑] 辅助类
(C++11 起) (直到 C++17) |
特化 std::uses_allocator 类型特征 (类模板特化) |
[编辑] 推导指引(自 C++17 起)
[编辑] 注释
当从不带尾随返回类型 (trailing-return-type) 的 lambda 表达式初始化结果类型为引用的 |
(直到 C++23) |
若从返回 prvalue 的函数或函数对象(包括不带尾随返回类型 (trailing-return-type) 的 lambda 表达式)初始化返回引用的 |
(自 C++23 起) |
std::function<const int&()> F([] { return 42; }); // Error since C++23: can't bind // the returned reference to a temporary int x = F(); // Undefined behavior until C++23: the result of F() is a dangling reference std::function<int&()> G([]() -> int& { static int i{0x2A}; return i; }); // OK std::function<const int&()> H([i{052}] -> const int& { return i; }); // OK
[编辑] 示例
运行此代码
#include <functional> #include <iostream> struct Foo { Foo(int num) : num_(num) {} void print_add(int i) const { std::cout << num_ + i << '\n'; } int num_; }; void print_num(int i) { std::cout << i << '\n'; } struct PrintNum { void operator()(int i) const { std::cout << i << '\n'; } }; int main() { // store a free function std::function<void(int)> f_display = print_num; f_display(-9); // store a lambda std::function<void()> f_display_42 = []() { print_num(42); }; f_display_42(); // store the result of a call to std::bind std::function<void()> f_display_31337 = std::bind(print_num, 31337); f_display_31337(); // store a call to a member function std::function<void(const Foo&, int)> f_add_display = &Foo::print_add; const Foo foo(314159); f_add_display(foo, 1); f_add_display(314159, 1); // store a call to a data member accessor std::function<int(Foo const&)> f_num = &Foo::num_; std::cout << "num_: " << f_num(foo) << '\n'; // store a call to a member function and object using std::placeholders::_1; std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1); f_add_display2(2); // store a call to a member function and object ptr std::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1); f_add_display3(3); // store a call to a function object std::function<void(int)> f_display_obj = PrintNum(); f_display_obj(18); auto factorial = [](int n) { // store a lambda object to emulate "recursive lambda"; aware of extra overhead std::function<int(int)> fac = [&](int n) { return (n < 2) ? 1 : n * fac(n - 1); }; // note that "auto fac = [&](int n) {...};" does not work in recursive calls return fac(n); }; for (int i{5}; i != 8; ++i) std::cout << i << "! = " << factorial(i) << "; "; std::cout << '\n'; }
可能输出
-9 42 31337 314160 314160 num_: 314159 314161 314162 18 5! = 120; 6! = 720; 7! = 5040;
[编辑] 参见
(C++23) |
任何可调用对象 (callable object) 的仅移动包装器,它在给定的调用签名中支持限定符 (类模板) |
(C++26) |
任何可复制构造可调用对象 (copy constructible callable object) 的可复制包装器,它在给定的调用签名中支持限定符 (类模板) |
(C++26) |
任何可调用对象 (callable object) 的非占有包装器 (类模板) |
(C++11) |
当调用空的 std::function 时抛出的异常 (类) |
(C++11) |
从成员指针创建函数对象 (函数模板) |
typeid | 查询类型信息,返回表示该类型的 std::type_info 对象 |