no member named 'bind2nd' in namespace 'std'
解决方案:将bind2nd函数修改为bind函数
std::bind2nd (std::ptr_fun (RequestQueue::IsEqual), dst))
修改为std::bind (std::ptr_fun (RequestQueue::IsEqual), std::placeholders::_1, dst))
在网上搜了一下,应该是bindend
已经被弃用了,所以要换成现在bind
bind函数std::bind是一个函数模板,就像是一个函数适配器,接受一个可调用对象,而生成一个新的可调用对象来适配原来的参数列表,该模板返回的是一个std::function对象。用该函数我们也能实现参数顺序的调整和给将指定参数设置成固定值。
占位符placeholder当分批付给函数参数的过程中,如果还没有赋值的参数,使用 std::placeholder 进行占位,意思是该位置的参数现在还不清楚,后期再赋值。
- 使用bind时,必须包含
functional.h
文件
#include <functional>
#include <iostream>
void foo(int a, int b, int c)
{
std::cout << "a = " << a << " b = " << b << " c = " << c << std::endl;
}
int main()
{
// 将参数 2,3 绑定到函数 foo 参数 b 和 c 上,但是使用 std::placeholders::_1 来对第一个参数进行占位。
auto bindFoo = std::bind(foo, std::placeholders::_1, 2, 3);
// 这时调用 bindFoo 时,只需要提供第一个参数即可
bindFoo(1);
return 0;
}
- bind函数实现参数的传递
// bind example
#include <iostream> // std::cout
#include <functional> // std::bind
// a function: (also works with function object: std::divides<double> my_divide;)
double my_divide (double x, double y) {
return x/y;}
int main () {
using namespace std::placeholders; // adds visibility of _1, _2, _3,...
// binding functions:
auto fn_1 = std::bind (my_divide,10,2); // returns 10/2 固定函数的两个参数值,分别为10和2
std::cout << fn_1() << '\n'; // 5
auto fn_2 = std::bind (my_divide,_1,2); // returns x/2 固定第二个参数值为2,第一个参数值由占位符确定
std::cout << fn_2(10) << '\n'; // 5
auto fn_3 = std::bind (my_divide,_2,_1); // returns y/x 利用bind函数更改传入参数的顺序,占位符2位置的参数由第二个参数确定
std::cout << fn_3(10,2) << '\n'; // 0.2
auto fn_4 = std::bind<int> (my_divide,_1,_2); // returns int(x/y) 顺序传入参数
std::cout << fn_4(10,3)