1、std::function
std::function 是 C++ 标准库中的一个类模板,包含在头文件<functional>
中,它用于封装可以调用的任意可调用对象,包括函数指针、函数对象、Lambda 表达式等。
std::function 提供了一种通用的方式来存储、复制和调用可调用对象,使得可以将它们作为参数传递给其他函数、存储在容器中,或者作为返回值返回。它是一种多态函数包装器,可以在运行时决定具体要调用的函数或函数对象。
std::function 的声明方式如下:
std::function<返回类型(参数类型)> func;
代码:保存普通函数和类成员函数
#include <iostream>
#include <functional>
void printA(int x)
{
std::cout << "printA x = " << x <<std::endl;
}
class Foo{
public:
Foo(int num) : num_(num){}
void printC(int x) const
{
std::cout << "printC x = " << num_ + x << std::endl;
}
int num_;
};
int main()
{
// 普通函数
std::function<void(int)> funcA;
funcA = printA;
funcA(22);
// lambda表达式
std::function<void()> funcB = []() { std::cout << "printB" << std::endl; };
funcB();
// 类成员函数
Foo foo(10);
std::function<void(const Foo&, int)> funcC = &Foo::printC;
funcC(foo, 1);
return 0;
}
结果:
printA x = 22
printB
printC x = 11
2、std::bind
std::bind的头文件是 <functional>,它是一个函数适配器,接受一个可调用对象(callable object),生成一个新的可调用对象来“适应”原对象的参数列表。
std::bind的声明方式如下:
template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
std::bind将可调用对象与其参数一起进行绑定,绑定后的结果可以使用std::function保存。std::bind主要有以下两个作用:
(1)将可调用对象和其参数绑定成一个防函数。
(2)只绑定部分参数,减少可调用对象传入的参数。
代码:
#include <iostream>
#include <functional>
float calcA(int x, int y)
{
return x / y;
}
class Foo{
public:
Foo(int num) : num_(num){}
float calcB(int x)
{
return num_ / x;
}
int num_;
};
int main()
{
// 普通函数
auto funcA = std::bind(calcA, std::placeholders::_1, 2);
std::cout << funcA(22) << std::endl;
/*
bind: 第一个参数是函数名,普通函数做实参时,会隐式转换成函数指针。
因此std::bind(calcA,_1,2)等价于std::bind(&calcA,_1,2);
_1表示占位符,位于<functional>中,std::placeholders::_1;
第一个参数被占位符占用,表示这个参数以调用时传入的参数为准,在这里调用calcA时,给它传入了22,其实就想到于调用calcA(22,2);
*/
auto funcB = std::bind(calcA, std::placeholders::_1, std::placeholders::_2);
std::cout << funcB(22, 10) << std::endl;
auto funcC = std::bind(calcA, 19, std::placeholders::_1);
std::cout << funcC(10) << std::endl;
auto funcD = std::bind(calcA, 19, 1);
std::cout << funcD() << std::endl;
// 类成员函数
Foo foo(10);
auto funcE = std::bind(&Foo::calcB, &foo, std::placeholders::_1);
std::cout << funcE(4) << std::endl;
/*
bind绑定类成员函数时,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址。
必须显式地指定&Base::calcB,因为编译器不会将对象的成员函数隐式转换成函数指针,所以必须在Base::calcB前添加&;
使用对象成员函数的指针时,必须要知道该指针属于哪个对象,因此第二个参数为对象的地址&foo;
*/
return 0;
}
结果:
11
2
1
19
2
3、std::function和std::bind结合使用
代码:
#include <iostream>
#include <functional>
void printA(int a, int b, int c) {
std::cout << "a + b + c = " << a + b + c << std::endl;
}
class Box {
public:
void printA(int a, int b, int c) {
std::cout << "a * b * c = " << a * b * c << std::endl;
}
};
int main() {
std::function<void(int, int)> f1 = std::bind(printA, std::placeholders::_1, std::placeholders::_2, 30);
f1(10, 20); // 输出:a + b + c = 6
Box BoxA;
std::function<void(int, int)> f2 = std::bind(&Box::printA, &BoxA, 40, std::placeholders::_1, std::placeholders::_2);
f2(10, 20); // 输出:a * b * c = 120
return 0;
}
结果:
a + b + c = 60
a * b * c = 8000
分析:
std::bind中有几个参数需要外部传入,则需要std::function指定几个参数类型。