目录
C/C++Linux服务器开发/后台架构师【零声教育】-学习视频教程-腾讯课堂
function和bind用法
在设计回调函数的时候,无可避免地会接触到可回调对象。在C++11中,提供了std::function和std::bind两个方法来对可回调对象进行统一和封装。
C++语言中有几种可调用对象:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调用运算符的类。和其他对象一样,可调用对象也有类型。例如,每个lambda有它自己唯一的(未命名)类类型;函数及函数指针的类型则由其返回值类型和实参类型决定。
function的用法
包含头文件:#include
1. 保存普通函数
//保存普通函数
void func1(int a)
{
cout << a << endl;
}
//1. 保存普通函数
std::function<void(int a)> func;
func = func1;
func(2); //2
2.保存lambda表达式
std::function<void()> func_1 = [](){cout << "hello world" << endl;};
func_1(); //hello world
3.保存成员函数
//保存成员函数
class A{
public:
A(string name) : name_(name){}
void func3(int i) const {cout <<name_ << ", " << i << endl;}
private:
string name_;
};
//3 保存成员函数
std::function<void(const A&,int)> func3_ = &A::func3;
A a("kaka");
func3_(a, 1);
Demo
#include <iostream>
#include <functional> // 添加头文件 functional
using namespace std;
// function 类似c的函数指针
//保存普通函数
void func1(int a)
{
cout << a << endl;
}
//保存成员函数
class A{
public:
A(string name) : name_(name){}
void func3(int i) const {cout <<name_ << ", " << i << endl;}
void func4(int k,int m)
{
cout<<"func4 print: k="<<k<<",m="<<m<<endl;
}
void func4(string str) {
cout<<"func4 print: str="<<str<<endl;
}
private:
string name_;
};
int main()
{
cout << "main1 -----------------" << endl;
//1. 保存普通函数
std::function<void(int a)> func1_ = func1;
func1_(2); //2
cout << "\nmain2 -----------------" << endl;
//2. 保存lambda表达式
std::function<void()> func2_ = [](){cout << "hello lambda" << endl;};
func2_(); //hello world
cout << "\nmain3 -----------------" << endl;
//3 保存成员函数
std::function<void(const A&, int)> func3_ = &A::func3;
A a("darren");
func3_(a, 1);
//4.重载函数
std::function<void(int, int)> func4_1 = std::bind((void(A::*)(int, int))&A::func4, a,std::placeholders::_1, std::placeholders::_2);
func4_1(1,2);
auto f_str = std::bind((void(A::*)(string))&A::func4, a,std::placeholders::_1);
f_str("kaka");
std::function<void(string)> f_str2 = std::bind((void(A::*)(string))&A::func4, &a,std::placeholders::_1);
f_str2("www");
return 0;
}
bind用法
可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
调用bind的一般形式:auto newCallable = bind(callable, arg_list);其中,newCallable本身是一个可调用对象,arg_list是一个逗号分隔的参数列表,对应给定的callable的参数。即,当我们调用newCallable时,newCallable会调用callable,并传给它arg_list中的参数。
arg_list中的参数可能包含形如n的名字,其中n是一个整数,这些参数是“占位符”,表示newCallable的参数,它们占据了传递给newCallable的参数的“位置”。数值n表示生成的可调用对象中参数的位置:1为newCallable的第一个参数,_2为第二个参数,以此类推。placeholders::_1.
Demo
#include <iostream>
#include <functional>
using namespace std;
void fun_1(int x, int y, int z) {
cout << "fun_1 print unchanged: x=" << x << ",y=" << y << ",z=" << z << endl;
}
void fun_2(int& a, int& b) {
a++;
b++;
cout << "fun_2 print Increment: a=" << a << ",b=" << b << endl;
}
class A {
public:
// 重载fun_3,主要bind的时候需要
// std::bind((void(A::*)(int, int))&A::fun_3
void fun_3(int k, int m) {
cout << "fun_3 a = " << a << "\t print unchanged: k=" << k << ",m=" << m << endl;
}
// std::bind((void(A::*)(string))&A::fun_3
void fun_3(string str) {
cout << "fun_3 print: str=" << str << endl;
}
int a;
};
int main() {
//f1的类型为 function<void(int, int, int)>
cout << "\nstd::bind(fun_1, 1, 2, 3) -----------------\n";
auto f1 = std::bind((void(*)(int, int, int))fun_1, 1, 2, 3); //表示绑定函数 fun 的第一,二,三个参数值为:1 2 3
f1(); //print: x=1,y=2,z=3
cout << "\n\nstd::bind(fun_1, 10, 20, 30) -----------------\n";
auto f1_1 = std::bind(fun_1, 10, 20, 30); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
f1_1();
cout << "\n\nstd::bind(fun_1, placeholders::_1,placeholders::_2, 3) -----------------\n";
auto f2 = std::bind(fun_1, placeholders::_1, placeholders::_2, 3);
//表示绑定函数 fun_1的第三个参数为 3,而fun_1的第一,二个参数分别由调用 f2 的第一,二个参数指定
f2(1, 2);//print: x=1,y=2,z=3
f2(10, 21, 30); // 传入30也没有用
cout << "\n\nstd::bind(fun_1,placeholders::_2,placeholders::_1,3) -----------------\n";
auto f3 = std::bind(fun_1, placeholders::_2, placeholders::_1, 3);
//表示绑定函数 fun_1 的第三个参数为 3,而fun_1的第一,二个参数分别由调用 f3 的第二,一个参数指定
//注意: f2 和 f3 的区别。
f3(1, 2);//print: x=2,y=1,z=3
cout << "\n\nstd::bind(fun_2, placeholders::_1, n) -----------------\n";
int m = 2;
int n = 3;
//表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
auto f4 = std::bind(fun_2, placeholders::_1, n); //func_2(3,<f4_1>)
f4(m); //print: m=3,n=4
cout << "m=" << m << endl;//m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m
cout << "n=" << n << endl;//n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n
cout << "\n\nstd::bind(&A::fun_3,&a1,40,50) -----------------\n";
A a;
a.a = 10;
//f5的类型为 function<void(int, int)>
auto f5 = std::bind((void(A::*)(int, int))&A::fun_3, &a, 40, 50);
f5(10, 20);//参数以及写死,传参没用
cout << "\n\nstd::bind(&A::fun_3, &a2,placeholders::_1,placeholders::_2) -----------------\n";
A a2;
a2.a = 20;
//f5的类型为 function<void(int, int)>
auto f6 = std::bind((void(A::*)(int, int)) & A::fun_3, &a2, placeholders::_1, placeholders::_2); //使用auto关键字
f6(10, 20);//调用a.fun_3(10,20),print: k=10,m=20
cout << "\n\nstd::bind(&A::fun_3,a3,std::placeholders::_1,std::placeholders::_2) -----------------\n";
std::function<void(int, int)> fc = std::bind((void(A::*)(int, int)) & A::fun_3, &a, std::placeholders::_1, std::placeholders::_2);
fc(10, 20); //调用a.fun_3(10,20) print: k=10,m=20
fc = std::bind((void(A::*)(int, int)) & A::fun_3, &a2, std::placeholders::_1, std::placeholders::_2);
cout << "\n\nstd::bind(&A::fun_3,&a,std::placeholders::_1) -----------------\n";
auto f_str = std::bind((void(A::*)(string)) & A::fun_3, &a, std::placeholders::_1);
f_str("kaka");
return 0;
}