测试functional的bind以及相关功能

注:在VS2010 UPDATE1下测试通过

 1 /*测试functional的bind以及相关功能*/
 2 
 3 #include <iostream>
 4 #include <functional>
 5 
 6 using namespace std;
 7 using namespace std::placeholders;
 8 
 9 int TestAdd(int a, int b)
10 {
11     return a+b;
12 }
13 bool TestCompare(int a, int b)
14 {
15     return a<b;
16 }
17 class TestBindClass
18 {
19 public:
20     bool operator() (const int &a, const int &b) const
21     {
22         cout << a << "<" << b << " ? " << ( a<b ? "true" : "false" ) << endl;
23         return a<b;
24     }
25     typedef int first_argument_type;
26     typedef int second_argument_type;
27     typedef bool result_type;
28 };
29 template <class T> struct greater1 
30 {
31     bool operator() (const T& x, const T& y) const 
32     {
33         cout << x << ">" << y << " ? " << ( x>y ? "true" : "false" ) << endl;
34         return x>y;
35     }
36     //void operator() (const T& a, const T& b) const {cout << "a<b ? " << ( a<b ? "true" : "false" ) << endl;}
37     typedef T first_argument_type;
38     typedef T second_argument_type;
39     typedef bool result_type;
40 };
41 
42 void main()
43 {
44     //test bind function
45     auto fcn1 = bind(TestAdd, 5, 10);
46     cout << fcn1() << endl;
47 
48     //test bind function
49     auto fcn2 = bind(TestCompare, _1, 5);
50     cout << fcn2(4) << endl;
51 
52     //test binder1st function
53     binder1st<greater1<int>> fcn4(greater1<int>(), 6);
54     cout << fcn4(7) << endl;
55 
56     //test binder1st function
57     binder1st<TestBindClass> fcn3(TestBindClass(), 5);
58     cout << fcn3(3) << endl;
59 }
煮酒论英雄
原文地址:https://www.cnblogs.com/superstargg/p/3712113.html