std::function
来自 cppreference.cn
定义于头文件 <functional> |
||
template< class > class function; /* 未定义 */ |
(C++11 起) | |
template< class R, class... Args > class function<R(Args...)>; |
(C++11 起) | |
类模板 std::function
是一个通用的多态函数封装器。std::function
实例可以存储、复制和调用任何可复制构造的(CopyConstructible)可调用对象(Callable) 目标——函数(通过其指针)、lambda 表达式、bind 表达式,或其他函数对象,以及指向成员函数的指针和指向数据成员的指针。
存储的可调用对象称为 std::function
的目标。如果 std::function
不包含目标,则称其为空的。调用空的 std::function
的目标会导致抛出 std::bad_function_call 异常。
std::function
满足可复制构造(CopyConstructible)和可复制赋值(CopyAssignable)的要求。
目录 |
[编辑] 成员类型
类型 | 定义 |
result_type
|
R
|
argument_type (在 C++17 中已弃用)(在 C++20 中已移除) |
如果 sizeof...(Args)==1 且 T 是 Args... 中的第一个也是唯一类型,则为 T |
first_argument_type (在 C++17 中已弃用)(在 C++20 中已移除) |
如果 sizeof...(Args)==2 且 T1 是 Args... 中两个类型中的第一个,则为 T1 |
second_argument_type (在 C++17 中已弃用)(在 C++20 中已移除) |
如果 sizeof...(Args)==2 且 T2 是 Args... 中两个类型中的第二个,则为 T2 |
[编辑] 成员函数
构造一个新的 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 开始)
[编辑] 注意
当 |
(直至 C++23) |
如果返回引用的 |
(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) |
任何支持给定调用签名中限定符的可调用对象的仅移动包装器 (类模板) |
(C++26) |
任何可复制构造的可调用对象的包装器,支持给定调用签名中的限定符 (类模板) |
(C++26) |
任何可调用对象的非拥有包装器 (类模板) |
(C++11) |
调用空的 std::function 时抛出的异常 (类) |
(C++11) |
从指向成员的指针创建函数对象 (函数模板) |
typeid | 查询类型信息,返回表示该类型的 std::type_info 对象 |