1.boost::bind()的使用方法(functions and function pointers)
定义如下函数:
int f(int a, int b)
{
return a + b;
}
int g(int a, int b, int c)
{
return a + b + c;
}
boost::bind(f, 1, 2) 可以产生一个无参函数对象(“nullary function object”),返回f(1,2)。类似地,bind(g,1,2,3)相当于g(1,2,3)。
bind(f,_1,5)(x)相当于f(x,5);_1是一个占位符,其位于f函数形参的第一形参int a的位置,5位于f函数形参int b的位置;_1表示(x)参数列表的第一个参数。
boost::bind可以处理多个参数:
bind(f, _2, _1)(x, y); // f(y, x)
bind(g, _1, 9, _1)(x); // g(x, 9, x)
bind(g, _3, _3, _3)(x, y, z); // g(z, z, z)
bind(g, _1, _1, _1)(x, y, z); // g(x, x, x)
参考文献
https://www.cnblogs.com/tiderfang/p/8968124.html