* In the introduction function before , Let's start with the following :
* Sometimes , Different types of objects may share the same call form . For example, the following three types , All share the same call form :int(int,int); int add(int i, int
j) { return i + j; } auto mod = [](int i, int j) {return i * j; }; struct
divide { int operator()(int denominator,int divisor) { return denominator /
divisor; } };
* To use these callable objects , We can define a function table to store the “ Pointer ”. We can go through map To achieve :
* parameter 1 Use one string To show what the object can be used for
* parameter 2 Is a function pointer map<string, int(*)(int,int)> binops; binops.insert({ "+",add
});// correct binops.insert({ "%",mod });// error
* And then the second is error , because lambda It's an expression , each lambda Each has its own class type , This type is stored in the binops The type of the value in does not match
One ,function Template introduction

* We can use it function The standard library type solves the above problems
In the new version of the standard library function Class and the unary_function and binary_function No connection , The last two classes have been more generic bind Function instead of

Two , use

* When using this template , Additional information must be provided to represent the calling form of the object that can be represented
Demonstration case

* function The template accepts two int, Return one int Callable object of function<int(int, int)> f1 = add;
function<int(int, int)> f2 = divide(); function<int(int, int)> f3 = [](int i,
int j) {return i*j; }; cout << f1(4,2)<< endl; //6 cout << f2(4, 2) << endl;//2
cout << f3(4, 2) << endl;//8
Redefinition map

* We use function The template redefines the map map<string, function<int(int, int)>> binops;
binops = { {"+",add}, {"-",std::minus<int>()}, {"/",divide()}, {"*",[](int
i,int j) {return i*j; }}, {"%",mod} }; binops["+"](10, 5); // call add(10,5);
binops["-"](10, 5); // use minus<int> The call operator of the object binops["/"](10, 5);
// use divide The call operator of the object binops["*"](10, 5); // call lambda Function object binops["%"](10, 5);
// call lambda Function object
Three , Overloaded functions and function Relationship of templates

* rule : You cannot directly save the name of an overloaded function into the function Object of type , There will be ambiguity
* resolvent : Store function pointer instead of function name in template
Wrong demonstration and correct case

* Error cases int add(int i, int j); long add(long i, long j); map<string,
function<int(int, int)>> binops; binops.insert({ "+",add });// error , Produce ambiguity
* The right way   int add(int i, int j); long add(long i, long j); int(*fp)(int, int) =
add; // Function pointer map<string, function<int(int, int)>> binops; binops.insert({ "+",fp
});// correct
same , We can also use it lambda To eliminate ambiguity .lambda Internal functions pass in two int, Therefore, the call can only match and accept two int Of add Function version , And this is execution lambda The function that is actually called
int add(int i, int j); long add(long i, long j); map<string, function<int(int,
int)>> binops; binops.insert({ "+",[](int a,int b) {return add(a,b); } })

©2019-2020 Toolsou All rights reserved,
QQ Login interface implementation code implementation mysql Addition, deletion, modification and query of database JAVA Experiment 4 set and functional programming experiment about String How to create objects VHDL——4 choose 1 Data selector C language ( Guess numbers games ) Blue Bridge Cup MCU advanced module --NE555 I don't memorize eight part essays , You go to the interview ? Hill sorting of sorting algorithm ——c++ realization python What does built-in function mean _python What are the built-in functions