绑定普通函数
绑定具体值
代码
#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;
}
测试结果: