传统回调函数
#include <iostream>
#include <functional>
using namespace std;
// 传统C函数
int c_function(int a, int b)
{
return a + b;
}
// 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
int main(int argc, char** argv)
{
int(*f)(int, int); // 声明函数类型,赋值只能是函数指针
f = c_function;
cout << f(3, 4) << endl;
Functor ff = Functor(); // 声明函数对象类型,赋值只能是函数对象
cout << ff(3, 4) << endl;
引入c++11 function
#include <functional>
#include <iostream>
using namespace std;
std::function< int(int)> Functional;
// 普通函数
int TestFunc(int a)
{
return a;
}
// Lambda表达式
auto lambda = [](int a)->int{ return a; };
// 仿函数(functor)
class Functor
{
public:
int operator()(int a)
{
return a;
}
};
// 1.类成员函数
// 2.类静态函数
class TestClass
{
public:
int ClassMember(int a) { return a; }
static int StaticMember(int a) { return a; }
};
int main()
{
// 普通函数
Functional = TestFunc;
int result = Functional(10);
cout << "普通函数:"<< result << endl;
// Lambda表达式
Functional = lambda;
result = Functional(20);
cout << "Lambda表达式:"<< result << endl;
// 仿函数
Functor testFunctor;
Functional = testFunctor;
result = Functional(30);
cout << "仿函数:"<< result << endl;
// 类成员函数
TestClass testObj;
Functional = std::bind(&TestClass::ClassMember, testObj, std::placeholders::_1);
result = Functional(40);
cout << "类成员函数:"<< result << endl;
// 类静态函数
Functional = TestClass::StaticMember;
result = Functional(50);
cout << "类静态函数:"<< result << endl;
return 0;
}
关于可调用实体转换为std::function对象需要遵守以下两条原则:
转换后的std::function对象的参数能转换为可调用实体的参数;
可调用实体的返回值能转换为std::function对象的返回值。
std::function对象最大的用处就是在实现函数回调(实际工作中就是用到了这一点),使用者需要注意,它不能被用来检查相等或者不相等,但是可以与NULL或者nullptr进行比较。
深入理解
#include <iostream>
#include <map>
#include <functional>
using namespace std;
// 普通函数
int add(int i, int j) { return i + j; }
// lambda表达式
auto mod = [](int i, int j){return i % j; };
// 函数对象类
struct divide
{
int operator() (int denominator, int divisor)
{
return denominator / divisor;
}
};
///SubMain//
int main(int argc, char *argv[])
{
// 受限的map
map<char, int(*)(int, int)> binops_limit;
binops_limit.insert({ '+', add });
binops_limit.insert({ '%', mod });
// 错误 1 error C2664: “void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert(std::initializer_list<std::pair<const _Kty,_Ty>>)”: 无法将参数 1 从“initializer-list”转换为“std::pair<const _Kty,_Ty> &&”
// binops_limit.insert({ '%', divide() });
// 更灵活的map
map<char, function<int(int, int)>> binops =
{
{ '+', add },
{ '-', minus<int>() },
{ '*', [](int i, int j){return i - j; } },
{ '/', divide() },
{ '%', mod },
};
cout << binops['+'](10, 5) << endl;
cout << binops['-'](10, 5) << endl;
cout << binops['*'](10, 5) << endl;
cout << binops['/'](10, 5) << endl;
cout << binops['%'](10, 5) << endl;
system("pause");
return 0;
}
///End Sub//
如上所示,function可以将普通函数,lambda表达式和函数对象类统一起来。它们并不是相同的类型,然而通过function模板类,可以转化为相同类型的对象(function对象),从而放入一个map里。
另外我实际测试的结果来看,在VS2013编译器下,上述代码可以通过,而第五版《C++ Primer》第512页第一行所言“错误:mod不是一个函数指针”并没有发生错误,有可能是对C++11标准的不同实现吧。
#include <iostream>
#include <functional>
using namespace std;
// 传统C函数
int c_function(int a, int b)
{
return a + b;
}
// 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
int main(int argc, char** argv)
{
// 万能可调用对象类型
std::function<int(int, int)> callableObject;
// 可以赋值为传统C函数指针
callableObject = c_function;
cout << callableObject(3, 4) << endl;
// 可以赋值为函数对象
Functor functor;
callableObject = functor;
cout << callableObject(3, 4) << endl;
// 可以赋值为lambda表达式(特殊函数对象)
callableObject = [](int a, int b){
return a + b;
};
cout << callableObject(3, 4) << endl;
#include <iostream>
#include <functional>
#include <list>
using namespace std;
// 传统C函数
int c_function(int a, int b)
{
return a + b;
}
// 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
int main(int argc, char** argv)
{
Functor functor;
std::list<std::function<int(int, int)>> callables;
callables.push_back(c_function);
callables.push_back(functor);
callables.push_back([](int x, int y)->int{
return x + y;
});
for (const auto& e : callables)
{
cout << e(3, 4) << endl;
}
}