1.function
std::function 对象是对C++中现有的可调用实体的一种类型安全的包裹(函数指针这类可调用实体,是类型不安全的)。可调用实体主要包括:
- 函数(全局函数, 静态函数)
- 函数指针
- 函数引用
- 隐式转换为函数指定的对象
实例:普通函数和仿函数的例子
#include <functional>
std::function< size_t (const char*)> PrintFunc;
//正常的函数 -> std::function object
//打印函数
size_t cprint(const char* .......)
{
printf(".........");
}
PrintFunc = cprint;
PrintFunc("Hello world");
打印结果: hello world
----------------------------------------------------
//functor(仿函数)--> std::function object
class cprint
{
public:
size_t operator()(const char*){.....}
};
cprint object;
PrintFunc = object
PrintFunc("hello world");
打印结果: hello world
注意事项:
- 关于可调用实体转换为std::function对象需要遵守以下两条原则:
a.转换后的std::function对象的参数能转换为可调用实体的参数
b.可高用实体的返回值能转换为std::function对象的(所有的可调用实体的返回值都与返回void的std::function对象的返回值兼容)
2.std::function对象可以refer to 满足(1)中条件的任意可调用实体
3.std::function object最大的用处就是在实现函数回调,使用者需要注意,不能被用来检查相等或者不相等
补充知识:仿函数(functor):就是使一个类的使用看上去像一个函数,其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数。
//less的定义
template<typename _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
bool
operator()(const _Tp& __x, const _Tp& __y) const
{ return __x < __y; }
};
//set的定义
template<typename _Key, typename _Compare = std::less<_Key>,
typename _Alloc = std::allocator<_Key> >
class set;
2.std::bind
2.1 std::bind1st 和 std::bind2nd
bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中颇为有用。c++98中,有两个函数bind1st和bind2st,他们分别用来绑定functor的第一个和第二个参数,他们都是只可以绑定一个参数。
例子:
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> coll;
for (int i = 1; i <= 10; ++i)
{
coll.push_back(i);
}
// 查找元素值大于10的元素的个数
// 也就是使得10 < elem成立的元素个数
// 意义: 从vector中找出大于10的数, 判断方法 less--> a < b
//其中, a固定为10, coll.begin() < b < coll.end()
int res = count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));
cout << res << endl;
// 查找元素值小于10的元素的个数
// 也就是使得elem < 10成立的元素个数
res = count_if(coll.begin(), coll.end(), bind2nd(less<int>(), 10));
cout << res << endl;
return 0;
}
解释:1. less<int>()其实是一个函数, 如果没有std::bind1st和std::bind2nd,使用less<int>,代码如下:
less<int> functor = less<int>();
bool bRet = functor(9, 20); // 返回true
less<int>()函数对象需要二个参数,比如上面 9 < 20进行比对, 其中9叫做 left参数,20叫做right参数。
- 当使用std::bind1st的时候,就表示绑定left参数,left参数不变,而right参数就是对应容器中的element.
- 当使用std::bind2nd的时候,就表示绑定right参数,right参数不变,而left参数就是对应容器中的element.
2.2定义
std::function绑定全局函数, 静态函数,std::bind绑定类的成员函数。
不借助std::bind, 传*this变量的例子:
#include <iostream>
#include <functional>
using namespace std;
class View
{
public:
void onClick(int x, int y)
{
cout << "X : " << x << ", Y : " << y << endl;
}
};
// 定义function类型, 三个参数
function<void(View, int, int)> clickCallback;
int main(int argc, const char * argv[])
{
View button;
// 指向成员函数
clickCallback = &View::onClick;
// 进行调用
clickCallback(button, 10, 123);
return 0;
}
使用std::bind的例子
#include <iostream>
#include <functional>
using namespace std;
int TestFunc(int a, char c, float f)
{
cout << a << endl;
cout << c << endl;
cout << f << endl;
return a;
}
int main()
{
auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
bindFunc1(10);
cout << "=================================\n";
auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
bindFunc2('B', 10);
cout << "=================================\n";
auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
bindFunc3(100.1, 30, 'c');
//打印: 30, C , 100.1
return 0;
}
上面的例子中,比如bindFunc3函数中:表示binFunc3中的第2个参数对应TestFunc中的第一个参数。bindFunc3中的第3个参数对应TestFunc中的第二个参数,bindFunc3中的第1个参数对应TestFunc中的第三个参数。
使用std::bind需要注意的地方:
- bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的。
- 对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的
- bind的返回值是可调用实体,可以直接赋给std::function对象。
- 对于绑定的指针,引用类型的参数,使用者需要保证在可调用实体之前,这些参数是可用的。
- 类的this可以通过对象或者指针来绑定