C + + function wrapper

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.