C++11 - function wrappers
Foreword:
Vue框架:
OJ算法系列:
Learn Vue magic tricks from the project - algorithm detailed explanation
Linux操作系统:
Fenghou Qimen - linux
function wrapper:
call object:
- Call object classification:
int ret = func(x);
//func可能是什么呢?
- Function name
- function pointer
- functor object
- lambda expression object
Template efficiency:
- Too many call objects, leading to inefficient use of templates:
template<class F, class T>
T useF(F f, T t){
static int count = 0; //一定为static
cout<<"count : "<<count++<<endl;
cout<<"&count : "<<&count<<endl;
return f(x);
}
//f的三种类型:
double func1(double x){
return i/2;
}
struct Functor{
double operator()(double x){
return x/3;
}
};
auto func2 = [](double x){
return x/4;
};
int main(){
useF(func1, 10);
useF(Functor(), 10);
useF(func2, 10);
return 0;
}
- Running result: three different count addresses
- Cause Analysis:
- class F corresponds to three different concrete types
- Instantiate three useF() functions
- Each useF() function contains its own static variable
Wrapper:
effect:
- Wrapping different callable objects into the same type
is also regarded as the same category when template recognition - It is equivalent to an increase in the type of the calling object,
especially the auto that was not easy to use, and
now the type can also be recorded as function<()> according to the parameters and return value;
usage:
- head File:
#include <functional>
- statement:
//对函数/函数指针:
function<double(double)> f1 = func1;
//对仿函数类:
function<double(double)> f2 = Functor();
//对lambda表达式:
function<double(double)> f3 = func2;
Wrapper class inner function:
Static function in wrapper class:
- Almost the same as wrapping a non-intrinsic function
class Calculator{
public:
int sum(int x, int y){
return x+y;
}
};
int main(){
function<int(int, int)> f4 = &Calculator::sum; //可不加&
return 0;
}
Non-static function in wrapper class:
- There are two differences from wrapping ordinary functions:
- plus ampersand
- Declare the class to which the static function belongs in the wrapper class
- An instance object needs to be passed in when calling
class Calculator{
public:
static int sub(int x, int y){
return x+y;
}
};
int main(){
function<int(Calculator, int, int)> f4 = &Calculator::sub;
f4(Calculator(), 1, 2);
return 0;
}
prove:
- Prove that after the wrapper, all functions belong to the same class:
#include <iostream>
#include <cstring>
#include <functional>
using namespace std;
template<class F, class T>
T useF(F f, T t){
static int count = 0; //一定为static
cout<<"count : "<<count++<<endl;
cout<<"&count : "<<&count<<endl;
return f(t);
}
double func1(double x){
return x/2;
}
struct Functor{
double operator()(double x){
return x/3;
}
};
auto func2 = [](double x){
return x/4;
};
class Test{
public:
static double sfunc(double x){
return x/5;
}
double dfunc(double x){
return x/6;
}
};
int main(){
function<double(double)> f1 = func1;
function<double(double)> f2 = Functor();
function<double(double)> f3 = func2;
useF(f1, 10);
useF(f2, 10);
useF(f3, 10);
function<double(double)> f4 = Test::sfunc;
function<double(Test, double)> f5 = &Test::dfunc;
useF(f4, 10);
cout<<"f5:"<<f5(Test(), 10);
/*
useF(f5, 10);
f5和f1~f4传参不同,所以 暂时 不能放入useF()函数中
学完绑定后即可
*/
return 0;
}
- operation result:
bind binding:
effect:
Adjust the order of parameter passing:
- placeholders::_n: Indicates that the current position value is passed to the nth parameter of the function
void func(int x, int y){
cout<<"x:"<<x <<" "<<"y:"<<y<<endl;
}
function<int(int ,int)> f1 = bind(func, placeholders::_1, placeholders::_2);
f1(1, 2);
function<int(int ,int)> f2 = bind(func, placeholders::_2, placeholders::_1);
f2(1, 2);
- Running result:
1 2
2 1
Adjust the number of parameters:
- For non-static functions in the class, bind the class + provide the object:
class Test{
public:
static double sfunc(double x){
return x/5;
}
double dfunc(double x){
return x/6;
}
};
function<double(Test, double)> f5 = &Test::dfunc;
f5(Test(), 10);
function<double(Test, double)> f6 = bind(&Test::dfunc, Test());
f6(10);
- Bind the non-static function + class object in the class to function() in one step