类模板 std::function
是一种通用多态函数包装器。std::function
的实例能存储、复制及调用任何可复制构造 (CopyConstructible) 的可调用 (Callable) 目标——函数(通过其指针)、lambda 表达式、bind 表达式或其他函数对象,以及成员函数指针和数据成员指针。
存储的可调用对象被称为 std::function
的目标。若 std::function
不含目标,则称它为空。调用空 std::function
的目标导致抛出 std::bad_function_call 异常。
std::function
满足可复制构造 (CopyConstructible) 和可复制赋值 (CopyAssignable) 。
成员函数
(构造函数) | 构造新的 std::function 实例 (公开成员函数) |
(析构函数) | 析构 std::function 实例 (公开成员函数) |
operator= | 赋值新的目标 (公开成员函数) |
swap | 交换内容 (公开成员函数) |
assign (C++17 中移除) | 赋值新的目标 (公开成员函数) |
operator bool | 检查是否包含目标 (公开成员函数) |
operator() | 调用其目标 (公开成员函数) |
目标访问 | |
target_type | 获得所存储目标的 typeid (公开成员函数) |
target | 获得指向所存储目标的指针 (公开成员函数) |
示例代码
#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 my_plus(int a, int b) { return a + b; }
int my_minus(int a, int b) { return a - b; }#include <typeinfo> // typeidint plus_function(int a, int b) { return a + b; }
int minus_function(int a, int b) { return a - b; }int main()
{// 存储自由函数std::function<void(int)> f_display = print_num;f_display(-9);// 存储 lambdastd::function<void()> f_display_42 = []() { print_num(42); };f_display_42();// 存储到 std::bind 调用的结果std::function<void()> f_display_31337 = std::bind(print_num, 31337);f_display_31337();// 存储到成员函数的调用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);// 存储到数据成员访问器的调用std::function<int(Foo const&)> f_num = &Foo::num_;std::cout << "num_: " << f_num(foo) << '\n';// 存储到成员函数及对象的调用using std::placeholders::_1;std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1);f_add_display2(2);// 存储到成员函数和对象指针的调用std::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1);f_add_display3(3);// 存储到函数对象的调用std::function<void(int)> f_display_obj = PrintNum();f_display_obj(18);auto factorial = [](int n){// 存储 lambda 对象以模拟“递归 lambda ”,注意额外开销std::function<int(int)> fac = [&](int n) { return (n < 2) ? 1 : n * fac(n - 1); };// 请注意 "auto fac = [&](int n){...};" 无法递归调用return fac(n);};for (int i{ 5 }; i != 8; ++i)std::cout << i << "! = " << factorial(i) << "; ";std::cout << '\n';// function::operator= examplestd::function<int(int)> foo1, bar1;foo1 = std::negate<int>(); // targetbar1 = foo1; // copyfoo1 = std::function<int(int)>([](int x) {return x + 1; }); // movebar1 = nullptr; // clearstd::cout << "foo1: " << foo1(100) << '\n';// function::swap examplestd::function<int(int, int)> foo2, bar2;foo2 = std::plus<int>();std::cout << "foo2(10,10) is " << foo2(10, 10) << '\n';foo2.swap(bar2);//std::cout << "foo2(10,10) is " << foo2(10, 10) << '\n';std::cout << "bar2(20,20) is " << bar2(20, 20) << '\n';//function::operator bool examplestd::function<int(int, int)> foo3, bar3;foo3 = std::plus<int>();std::cout << "foo3 is " << (foo3 ? "callable" : "not callable") << '\n';std::cout << "bar3 is " << (bar3 ? "callable" : "not callable") << '\n';//function::operator() example// an array of functions:std::function<int(int, int)> fnArray[] = {std::plus<int>(),std::minus<int>(),std::multiplies<int>()};for (auto& x : fnArray) std::cout << x(10, 5) << '\n';// function::target examplestd::function<int(int, int)> foo5 = my_plus;std::function<int(int, int)> bar5 = std::plus<int>();// calling using functional form:std::cout << foo5(100, 20) << '\n';std::cout << bar5(100, 20) << '\n';// calling by invoking target:std::cout << (*foo5.target<int(*)(int, int)>())(100, 20) << '\n';std::cout << (*bar5.target<std::plus<int>>())(100, 20) << '\n';// changing target directly:*foo5.target<int(*)(int, int)>() = &my_minus;std::cout << foo5(100, 20) << '\n';// function::target_type examplestd::function<int(int, int)> plus1 = plus_function;std::function<int(int, int)> plus2 = std::plus<int>();std::function<int(int, int)> minus1 = minus_function;std::function<int(int, int)> minus2 = std::minus<int>();std::cout << "pointers as targets:\n" << std::boolalpha;std::cout << "plus1 : " << (plus1.target_type() == typeid(int(*)(int, int))) << '\n';std::cout << "plus2 : " << (plus2.target_type() == typeid(int(*)(int, int))) << '\n';std::cout << "minus1: " << (minus1.target_type() == typeid(int(*)(int, int))) << '\n';std::cout << "minus2: " << (minus2.target_type() == typeid(int(*)(int, int))) << '\n';std::cout << '\n';std::cout << "same type?:\n";std::cout << "(plus1, plus2) : " << (plus1.target_type() == plus2.target_type()) << '\n';std::cout << "(minus1,minus2): " << (minus1.target_type() == minus2.target_type()) << '\n';std::cout << "(plus1, minus1): " << (plus1.target_type() == minus1.target_type()) << '\n';std::cout << "(plus2, minus2): " << (plus2.target_type() == minus2.target_type()) << '\n';return 0;
}
运行结果:
参考:
https://cplusplus.com/reference/functional/
标准库头文件 <functional> - cppreference.com