An introduction to std::function
The class template std::function is a general polymorphic function wrapper. Instances of std::function can store, copy and call any callable target: including functions, lambda expressions, binding expressions or other function objects, as well as pointers to member functions and to data members. When the std::function object does not wrap any actual callable elements, calling the std::function object will throw a std::bad_function_call exception.
Introduction to bind
Function binding The bind function is used to bind a certain form of parameter list with a known function to form a new function. This method of changing the existing function call mode is called function binding. Need to point out: bind is a function adapter. Adapter is a mechanism to modify and restrict existing things so that it can adapt to new logic.
It accepts a callable object and generates a new callable object to adapt to the parameter list of the original object.
The general form used by bind:
auto newFun = bind(oldFun,arg_list);
Three examples
#include <functional>
#include <iostream>
int show(string name, int age)
{
cout << "name:" << name << "\tage:"<<age << endl;
return 0;
}
int main()
{
// std::function
using namespace std::placeholders;
std::function<int(string, int)> func = show;
func("张A", 100);
//Lamda
std::function<int(string, int)> funb = [](string name, int age)->int{
cout << "name:" << name << "\tage:" << age << endl;
return 0;
};
funb("张B", 95);
//bind
//在bind函数后有一个符号_1,这是一个占位符,代表绑定函数中的第一个参数,_2同理
auto retfunc = bind(show, _2, _1);//相当于int show(int age, string name)
retfunc(90,"张C");
auto retfund = bind(show, _1, _2);//原函数
retfund("张D", 85);
auto retfune = bind(show, _1, 80);//相当于int show(int age)
retfune("张E");
auto retfunf = bind(show, "张F",75);//相当于int show()
retfunf();
system("pause");
return 0;
}
result
Four function usage scenarios
#include <iostream>
#include <functional>
using namespace std;
std::function<bool(int, int)> fun;
//普通函数
bool compare_com(int a, int b)
{
return a > b;
}
//lambda表达式
auto compare_lambda = [](int a, int b) {
return a > b; };
//仿函数
class compare_class
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
//类成员函数
class compare
{
public:
bool compare_member(int a, int b)
{
return a > b;
}
static bool compare_static_member(int a, int b)
{
return a > b;
}
};
int main()
{
bool result;
fun = compare_com;
result = fun(10, 1);
cout << "普通函数输出, result is " << result << endl;
fun = compare_lambda;
result = fun(10, 1);
cout << "lambda表达式输出, result is " << result << endl;
fun = compare_class();
result = fun(10, 1);
cout << "仿函数输出, result is " << result << endl;
fun = compare::compare_static_member;
result = fun(10, 1);
cout << "类静态成员函数输出, result is " << result << endl;
//类普通成员函数比较特殊,需要使用bind函数,并且需要实例化对象,成员函数要加取地址符
compare temp;
fun = std::bind(&compare::compare_member,&temp, std::placeholders::_1, std::placeholders::_2);
result = fun(10, 1);
cout << "类普通成员函数输出, result is " << result << endl;
return 0;
}
The code is taken from function