49.函数包装器

 1 #include <iostream>
 2 #include <functional>
 3 using namespace std;
 4 using std::function;
 5  
 6 int add(int a, int b)
 7 {
 8     return a + b;
 9 }
10 
11 template<class T,class F>
12 T run(T t1, T t2, F f)
13 {
14     return f(t1, t2);
15 }
16 
17 void main()
18 {
19     function<int(int, int)> fun1 = add;
20     function<int(int, int)> fun2 = [](int a,int b)->int {return a - b; };
21 
22     cout << fun1(10, 12) << endl;
23     
24     cout << run(10, 12, fun1) << endl;//默认推导
25     //精确赋予类型
26     cout << run<int, function<int(int, int)>>(10, 20, fun1) << endl;
27     cin.get();
28 }

 

posted @ 2018-03-12 23:05  喵小喵~  阅读(78)  评论(0编辑  收藏  举报