[1] std :: function Introduction
std :: function is a wrapper function template, the earliest from the boost library, boost :: function corresponding to its function wrapper.
A std :: function type of object instances can be called the following packaging element type, and so on:
(1) Function
(2) function pointer
(3) member function pointers
(4) any type of function objects (e.g.: defines the operator () the type of operation).
std :: function object can be copied and transferred, and can be used to invoke specific features of call target element directly.
When std :: function objects can not wrap any actual call to the elements, call the std :: function object will throw std :: bad_function_call exception.
[2] using std :: funciton
1 #include <iostream> 2 #include <functional> 3 using namespace std; 4 5 int subtract(int m, int n) 6 { 7 return (m - n); 8 } 9 10 template <class T> 11 T g_sub(T m, T n) 12 { 13 return (m - n); 14 } 15 16 auto g_Lambda = [](int m, int n) . 17 { 18 is return (m - n-); . 19 }; // NOTE: anonymous functions where there is a semicolon 20 is 21 is struct Sub 22 is { 23 is int operator () ( int m, int n-) 24 { 25 return (m - n- ); 26 is } 27 }; 28 29 Template < class T> 30 struct SubTemp 31 is { 32 T operator () (T m, n-T) 33 { 34 return (m - n); 35 } 36 }; 37 38 class SubOper 39 { 40 public: 41 static int st_sub(int m, int n) 42 { 43 return (m - n); 44 } 45 46 template <class T> 47 static T temp_sub(T m, T n) 48 { 49 return (m - n); 50 } 51 52 double result(double m, double n) 53 { 54 return (m - n); 55 } 56 57 double const_result(double m, double n) const 58 { 59 return (m - n); 60 } 61 }; 62 63 int main() 64 { 65 // 旧式写法 66 typedef int (*pFunc) (int, int); 67 pFunc oldFunc = subtract; 68 cout << "Test old style :: " << (*oldFunc)(9, 10) << endl; // -1 69 70 // [0] 包装函数指针对象 71 std::function<int(int, int)> from_pFunc = oldFunc; 72 cout << "Test0 :: " << from_pFunc(10, 10) << endl; // 0 73 is 74 // [. 1] Common packaging function 75 STD :: function < int ( int , int )> = newfunc Subtract; 76 COUT << " Test1 :: " << newfunc ( . 11 , 10 ) << endl; // . 1 77 78 // [2] packaging template function 79 STD :: function < int ( int , int )> = g_sub tempFunc < int > ; 80 COUT << "Test2 :: " << tempFunc(12, 10) << endl; // 2 81 82 // [3]包装Lambda函数 83 std::function<int(int, int)> lambdaFunc = g_Lambda; 84 cout << "Test3 :: " << lambdaFunc(13, 10) << endl; // 3 85 86 // [4]包装仿函数 87 std::function<int(int, int)> objFunc =Sub (); 88 COUT << " Test4 :: " << objFunc ( 14 , 10 ) << endl; // . 4 89 90 // [. 5] packaging template function object 91 is STD :: function < int ( int , int )> = SubTemp tempFuncObj < int > (); 92 COUT << " Test5 should be conducted :: " << tempFuncObj ( 15 , 10 ) << endl; // . 5 93 94 // [. 6] static class functions 95 std::function<int(int, int)> stFunc = &SubOper::st_sub; 96 cout << "Test6 :: " << stFunc(16, 10) << endl; // 6 97 98 // [7] 类静态模板函数 99 std::function<int(int, int)> tempSTFunc = &SubOper::temp_sub<int>; 100 cout << "Test7 :: " << tempSTFunc(17, 10) << endl; // . 7 101 102 // [. 8] Normal function class (normal binding function relies on the class object) 103 SubOper subOperObject; 104 105 // [8.1] use bind, bind to the class object address 106 :: function STD < Double ( Double , Double )> = STD :: resultFunc the bind (Result :: & SubOper, & subOperObject, placeholders. 1 :: _, _ :: placeholders 2); 107 COUT << " Test8.1 :: " < <resultFunc ( 18.2 , 10.1 ) << endl; // 8.1 108 109 // [8.2] does not use the bind 110 std::function<double(SubOper &, double, double)> resultFunc2 = &SubOper::result; 111 cout << "Test8.2 :: " << resultFunc2(subOperObject, 18.3, 10.1) << endl; // 8.2 112 113 // [8.3] const 114 std::function<double(SubOper &, double, double)> const_resultFunc2 = &SubOper::const_result; 115 cout << "Test8.3 :: " << const_resultFunc2(subOperObject, 18.4, 10.1) << endl; // 8.3 116 117 // [8.4] 常量对象 118 const SubOper subOperConst; 119 std::function<double(const SubOper &, double, double)> const_Func2 = &SubOper::const_result; 120 cout << "Test8.4 :: " << const_Func2(subOperConst, 18.5, 10.1) << endl; // 8.4 121 122 // [9] 应用示例(为了解耦) 123 class TestA 124 { 125 public: 126 bool destoryByName(const std::string& name) 127 { 128 return doDestoryByName(name); 129 } 130 131 public: 132 std::function<bool(const std::string&)> destory_handler; 133 134 private: 135 bool doDestoryByName(std::string name) 136 { 137 return destory_handler(name); 138 } 139 }; 140 141 class TestB 142 { 143 public: 144 bool destory(const std::string& name) 145 { 146 cout << "Test9 :: Call TestB destory | name : " << name << endl; 147 return true; 148 }; 149 }; 150 151 TestB, objB; 152 TestA objA; 153 objA.destory_handler = [&] ( const STD :: String & name) -> BOOL { 154 // destroy operations 155 return objB.destory (name); 156 }; 157 objB.destory ( " Kaizen " ); 158 159 // [10] when an abnormal operation is empty 160. STD :: function < int ( int , int )> dealWithFunc; 161 162 the try 163 { 164 if (nullptr == dealWithFunc) 165 { 166 throw runtime_error("std::bad_function_call"); //抛出异常 167 } 168 else 169 { 170 dealWithFunc(100, 10); 171 } 172 } 173 catch (exception e) 174 { 175 cout << "Test10 :: "E.what << () << endl; // catch the exception, then the program ends 176 } 177 178 System ( " PAUSE " ); 179 } 180 [ 181 / * Rusult 182 the Test :: -1 Old style 183 is test0 :: 0 184 Test1. 1 :: 185 Test2 :: 2 186 the Test3 ::. 3 187 Test4. 4 :: 188 Test5 should be conducted. 5 :: 189 Test6. 6 :: 190 TEST7. 7 :: 191 Test8.1 8.1 :: 192 Test8.2 8.2 :: 193 :: 8.3 Test8.3 194 :: 8.4 Test8.4 195 test9 :: Call TestB Destory | name: Kaizen 196 test10 :: std :: bad_function_call 197 Press any key to continue... 198 * /
good good study, day day up.
Select the cycle order summary