std::bind绑定普通函数、模板、类成员函数、类模板函数测试和简单策略

绑定普通函数

绑定具体值

代码

#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

int add_func(int a, int b) {
	return a + b;
}

int main() {
	auto func = std::bind(add_func, 5, 10);
	cout << func() << endl;
	return 0;
}

测试结果

使用占位符

代码

#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

int add_func(int a, int b) {
	return a + b;
}

int main() {
	auto func = std::bind(add_func, std::placeholders::_1, std::placeholders::_2);
	cout << func(10,15) << endl;
	cout << func(15, 25) << endl;
	return 0;
}

测试结果

绑定模板

代码

#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;
template <typename T>
T add_func(T a, T b) {
	return a + b;
}

int main() {
	auto func_int = std::bind(add_func<int>, std::placeholders::_1, std::placeholders::_2);
	cout << func_int(10,15) << endl;
	cout << func_int(15, 25) << endl;

	auto func_float = std::bind(add_func<double>, std::placeholders::_1, std::placeholders::_2);
	cout << func_float(10.5, 15.6) << endl;
	cout << func_float(1.1, 2.5) << endl;

	auto func3 = std::bind(add_func<string>, string("hello "), string("world"));
	cout << func3() << endl;
	return 0;
}

测试结果

绑定成员函数

普通类

代码


#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

class A {
public:
	int add(int a, int b) {
		return a + b;
	}
};

int main() {
	A a;
	auto func_1 = std::bind(&A::add, &a,1,2);
	cout << func_1() << endl;

	auto func_2 = std::bind(&A::add, &a, std::placeholders::_1, std::placeholders::_2);
	cout << func_2(3,4) << endl;
	return 0;
}

测试结果

模板类

测试代码

#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

template <typename T>
class A {
public:
	T add(T a, T b) {
		return a + b;
	}
};

int main() {
	A<int> a1;
	auto func_1 = std::bind(&A<int>::add, &a1,1,2);
	cout << func_1() << endl;

	A<int> a2;
	auto func_2 = std::bind(&A<int>::add, &a2, std::placeholders::_1, std::placeholders::_2);
	cout << func_2(3,4) << endl;

	A<double> a3;
	auto func_3 = std::bind(&A<double>::add, &a3, std::placeholders::_1, std::placeholders::_2);
	cout << func_3(3.3, 4.5) << endl;
	return 0;
}

测试结果

将成员函数作为另一个类的回调函数(简单策略)

测试代码:

定义A类、C类和B类,本实验中,将A类和C类是函数实现者,B类是函数调用者。

// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

template <typename T>
class A {
public:
	T add(T a, T b) {
		return a + b;
	}
};
template <typename T>
class C {
public:
	T subtraction(T a, T b) {
		return a - b;
	}
};

template<typename T>
class B {
private:
	std::function<T(T, T)> callback;
public:	
	void set_callback(std::function<T(T, T)> _callback) {
		this->callback = _callback;
	}
	void clear_callback(std::function<T(T, T)> _callback) {
		this->callback = std::function<T(T, T)>();
	}
	void test(T a, T b) {
		if (callback == nullptr) {
			cout << "null function" << endl;
			return;
		}
		else {
			cout << "callback is ok" << endl;
		}
		cout << callback(a, b) << endl;
	}
};

int main() {
	A<int> a1;
	auto func_1 = std::bind(&A<int>::add, &a1,1,2);
	cout << func_1() << endl;
	B<int> b;
	b.test(10,11);
	auto func_2 = std::bind(&A<int>::add, &a1, std::placeholders::_1, std::placeholders::_2);
	b.set_callback(func_2);
	b.test(12,13);

	C<int> c;
	auto func_3 = std::bind(&C<int>::subtraction, &c, std::placeholders::_1, std::placeholders::_2);
	b.set_callback(func_3);
	b.test(14,15);
	return 0;
}

测试结果:

小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值