C++ 函数封装与绑定

STL标准库中提供了一些函数包装的模板

它们可以对函数或者可调用的对象进行包装,方便在其他函数中调用

std::function

std::function是一个通用的多态函数封装器

它将一个可调用的对象,比如函数指针、函数对象、lambda函数等进行封装,方便在后续的代码中调用

std::function的定义:
复制代码
template<class R,class... Args>
class function<R<Args...>>;

// R<Args...> 这种定义方式是类模板的部分特化,可以将它拆成两个部分
//R 是函数返回类型
//Args是函数参数类型,...说明Args是一个可变模板参数,代表模板可以接受任意多个参数
std::function使用:

定义函数封装器对象

复制代码
function<R<Args...>> fname = target;

#include<iostream>
#include<functional>

double multiply(double a,double b)
{
	return a+b;
};

int main()
{
	std::function<double(double,double)> func1 = multiply;
	std::cout<< func1(4,5) <<std::endl;
	return 0;
}
封装类成员函数

在封装类成员函数时,第一个参数要传入类引用

复制代码
class Calc
{
public:
	Calc(int Inbase):base(Inbase){	}
	double multiplay(double a,double b){ return a * b; }
	double add(double a,double b){ return a + b; }
	
	int base;
};

int main()
{
	Calc c(123);
	
	//第一个参数要传入类引用
	std::function<double(Calc&,double,double)> Mfunc = &Calc::multiplay;
	std::cout<< Mfunc(c,4,5) <<std::endl;
	//也可以封装成员变量
	std::function<int(Calc&)> Mbase = &Calc::base;
	std::cout<< Mbase(c) <<std::endl;
	
	return 0;
}
类型擦除

std::function只需定义一个函数封装器对象,就可以接收函数指针、函数对象、lambda函数,只要它们的函数签名相同

复制代码
#include<iostream>
#include<functional>
#include<map>

double add(double a,double b)
{
	return a+b;
}
class Substract
{
public:
	double operator()(double a,double b){ return a - b; }
};

int main()
{
	std::map<char,std::function<double(double,double)>> calculator{
		{ '+',add},
		{ '-',Substract()},
		{ '*',[](double a,double b)->double {return  a*b;}}
	};
	std::cout<< calculator['+'](1.1,2)<<std::endl;
	std::cout<< calculator['-'](5,3)<<std::endl;
	std::cout<< calculator['*'](4,6)<<std::endl;
	return 0;
}

std::mem_fn

它主要是封装类成员函数,传入成员函数的地址,返回包装器

复制代码
#include<iostream>
#include<functional>


class Calc
{
public:
	double multiplay(double a,double b){ return a * b; }
	double operator+=(double a){ return a + 10; }
};

int main()
{
	Calc c;
	auto Mfunc1 = std::mem_fn(&Calc::multiplay);
	std::cout<<Mfunc1(c,1,2)<<std::endl;

	//可以封装运算符重载的函数
	auto Mfunc2 = std::mem_fn(&Calc::operator+=);
	std::cout<<Mfunc2(c,4)<<std::endl;
	return 0;
}

std::bind

std::bind 可以将函数包装成函数对象(仿函数)

绑定参数
复制代码
int sum(int a,int b,int c)
{
	std::cout<<"a: "<<a <<" b: "<<b <<" c: "<<c;
	return a+b+c;
}

int main()
{
	//传入函数,与要绑定的参数值
	auto f = std::bind(sum,1,2,3);
	int result = f();              //绑定的函数对象调用
	std::cout<< " sum: " <<result; //结果: a: 1 b: 2 c: 3 sum: 6
	return 0;
}
使用占位符

可以使用 std::placeholders 中的占位符(_1,_2,_3 ...)来代替被绑定的实际参数

复制代码
#include<iostream>
#include<functional>
using namespace std::placeholders;

int sum(int a,int b,int c)
{
	std::cout<<"a: "<<a <<" b: "<<b <<" c: "<<c;
	return a+b+c;
}

int main()
{
	auto f = std::bind(sum,1,_1,3);
	int result = f(5);
	std::cout<< " sum: " <<result;  //结果: a: 1 b: 5 c: 3 sum: 9
	return 0;
}
使用变量作为绑定参数

注意变量是作为值传入的,因此后续修改变量,不会影响到函数

复制代码
#include<iostream>
#include<functional>
using namespace std::placeholders;

int sum(int a,int b,int c)
{
	std::cout<<"a: "<<a <<" b: "<<b <<" c: "<<c;
	return a+b+c;
}

int main()
{
	int n = 6;
	auto f = std::bind(sum,1,2,n);
	n = 99;
	int result = f(5);
	std::cout<< " sum: " <<result;  //结果: a: 1 b: 2 c: 6 sum: 9
	return 0;
}

如果想要变量以引用方式传入,需要使用std::cref();

复制代码
	int n = 6;
	auto f = std::bind(sum,1,2,std::cref(n));
	n = 99;
	int result = f(5);
	std::cout<< " sum: " <<result;  //结果: a: 1 b: 2 c: 99 sum: 102
	return 0;

注:cref()ref()其中 c 指的是const

相关推荐
xlp666hub18 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网20 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub1 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星1 天前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub2 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
不想写代码的星星2 天前
C++继承、组合、聚合:选错了是屎山,选对了是神器
c++
不想写代码的星星3 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
樱木Plus5 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit7 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_8 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++