函数包装器function:
1. 特征标:是由返回类型以及括号括起并用逗号分隔的参数类型列表定义的。
eg: 返回类型(参数1类型, 参数2类型,…)
2. 函数包装器function:是在头文件functional中声明的,它从调用特征标的角度定义了一个对象,可用于包装调用特征标相同的函数指针、函数对象或lambda表达式。
3. function语法:function<返回类型(参数1类型, 参数2类型,…)> fc;
例如,下面声明创建一个名为fc的function对象,它接受一个char参数和一个in参数,并返回一个double值:
std::function<double(char, int)> fc;
它接受一个char参数和一个int参数,并返回一个double值的任何函数指针,函数对象或lambda表达式赋给它。
4. function使用:
#include <iostream>
#include <functional>
#include <list>
using namespace std;
// 传统C函数
int c_function(int a, int b)
{
return a + b;
}
// 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
int main(int argc, char** argv)
{
int n = 100;
const type_info &nInfo = typeid(n);
Functor functor;
std::list<std::function<int(int, int)>> callables;
callables.push_back(c_function);
callables.push_back(functor);
callables.push_back([](int x, int y)->int {
return x + y;
});
function<int(int, int)> fc = c_function;
int test = fc(6, 6);
cout << "test: " << test << endl;
for (const auto& e : callables)
{
cout << e(3, 4) << endl;
}
system("pause");
}
函数适配器bind:
1. bind概念:可以看作一个通用的函数适配器,该函数可以对函数指针、函数对象、lambda表达式和函数所需的参数(任意个数)进行绑定,生成一个新的可调用对象来“适应”原对象的参数列表。绑定后的返回值可以使用std::function保存,function类型与所绑定的函数特征标一致,也可使用auto关键字来保存。
2. bind语法:function<特征标> fc = bind(调用对象, 参数1, 参数2, …);
返回值function对象fc的特征标与bind的调用对象一致。参数1,参数2为调用对象的参数列表,可以在bind时指定具体参数值,也可使用占位符std::placeholders::_n,待调用fc的时候传入。 注:当bind成员函数时,调用对象为成员函数,参数1应为具体对象或指针,参数2开始对象成员函数的参数列表。
eg: 下面定义了一个函数,然后使用bind返回一个function对象。
int example(int x) { return x; }
function<int(int)> fc = bind(&example, std::placeholders::_1);
std::cout << fc(10);
最后一行相当于std::cout<<example(10),也可以bind时候指定具体参数,这种情况只能使用auto来获得返回值。
auto fc = bind(&example, 8);
std::cout << fc();
最后一行调用fc忽略了参数,相当于std::cout<<example(8)。
3. 绑定参数方式:
1). 改变参数个数,绑定全局函数:
int f(int, char, double);
auto ff = bind(f, _1, 'c', 1.2); // 绑定f()函数调用的第二个和第三个参数,返回一个新的函数对象为ff,它只带有一个int类型的参数
int x = ff(7);
2). 改变参数的顺序
int f(int, char, double);
auto frev = bind(f, _3, _2, _1); // 翻转参数顺序
int x = frev(1.2, 'c', 7); // f(7, 'c', 1.2);
有以下类:
class student {
private:
string name;
int age;
public:
static void showpersonms(string name, int age) {
cout << "my name is " << name << ",and I am " << age << " years old!\n";
}
void introduce(string name, string addr) {
cout << "my name is " << name << " and my addr is " << addr << endl;
}
};
3). 绑定静态成员函数
auto pfx = bind(student::showpersonms, std::placeholders::_1, std::placeholders::_2);
pfx("jack", 27);
4). 绑定普通成员函数
student s;
auto myfun = std::bind(&student::introduce, s, std::placeholders::_1, std::placeholders::_2);
bind的使用:
#include <iostream>
#include <functional>
using namespace std;
class AA
{
public:
AA() { }
~AA() { }
void showA(int b) {
cout << b << endl;
}
static void showAA(int b) {
cout << b << endl;
}
int m_a;
};
void show1(AA a, int x, int y) {
a.m_a = 2;
cout << a.m_a << x << y << endl;
}
void show2(AA &a, int x) {
a.m_a = 30;
cout << a.m_a << x << endl;
}
int main()
{
AA a;
a.m_a = 66;
//bind普通函数show1,bind时使用占位符
//可以使用function对象,也可以使用auto
function<void(AA, int, int)> func1 = bind(show1, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
//auto func1 = bind(&show2, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
func1(a, 6, 6);
//bind普通函数show2
//bind时直接将非占位符a传入,则会进行拷贝构造,传入a的拷贝(注意:是bind时拷贝构造)
//若想将a本身进行传参,需加上ref
auto func2 = bind(show2, ref(a), std::placeholders::_1);
func2(20);
//bind成员函数&AA::showA
//第二位需指定具体的类对象,可以是指针可以是对象实例
auto func3 = bind(&AA::showA, a, 8);
func3();//相当于调用a.*showAA(8);
//bind静态成员函数&AA::showA
//第二位需指定具体的类对象,可以是指针可以是对象实例
auto func3 = bind(AA::showAA, 8);
func3();//相当于调用a.*showAA(8);
system("pause");
return 0;
}
参考:
https://blog.csdn.net/qq_35905572/article/details/96266145
https://blog.csdn.net/FairLikeSnow/article/details/103794918