The function wrapper wraps up the function: it has the following characteristics
1. Design a common function execution interface, you can set the count (number of function executions) and level
2. Function wrappers rely on function templates to implement generic generics
3. Function code can be embedded in the function
4. The principle is the function pointer implementation
The following is a simple example of a function wrapper for C + +
#include <iostream> #include <functional>using std::cout;using std::endl;using std::cin;using std:: function;//defines a function wrapper template < typename T, TypeName F>t function_wrapper (T T, F Fun) {return fun (t);} I've defined a function wrapper template < typename T, TypeName F>t function_wrapper (t t1, T t2,f fun) {static int count = 0;count++;cou T << "function wrapper execution" << count << "number" << Endl;return fun (T1,T2);} void Main () {double d_num = 1.1;//double (double) is the declaration function type//allow function inline//essence is function pointer//[] identify to open a function function<double (double) > Square_fun = [] (double in_data) {return in_data*in_data;};/ /function<double (double) > Cube_fun = [] (double in_data) {return in_data*in_data*in_data;}; Function<int (int,int) > Add_fun = [] (int in_data1,int in_data2) {return in_data1+in_data2;}; cout << function_wrapper (D_num, Square_fun) << "" << function_wrapper (D_num, cube_fun) << endl;c Out << function_wrapper (1,2,add_fun) << endl;cin.get ();}
C + + function wrapper