一、std::function和std::bind与回调函数
回调函数,简单的理解是,类里面有成员可以调用类对象本身(可以理解为this指针)
包含知识点(函数指针、重载operator()、*this)
1. 不同标题类型可能具有相同的调用形式,如:
// 普通函数
int add(int a, int b){return a+b;}
// lambda表达式
auto mod = [](int a, int b){ return a % b;}
// 函数对象类
struct divide{
int operator()(int denominator, int divisor){
return denominator/divisor;
}
};
思考:但是三者都有一个共同的参数形式 int(int,int),所以想办法将三者用同一个函数指针调用起来
std::function<int(int ,int)> a = add;
std::function<int(int ,int)> b = mod ;
std::function<int(int ,int)> c = divide();
思考:std::function是一个函数模板类,std::bind是一个模板函数
前者是一个类,没有实体对象,泛化的函数,写成模板形式,所以需要传入参数列表,然后实体化–>一个类
后者本质上是一个函数,将模板放到函数里面,打包。让外面的函数类调用。–>一个函数实体
2.std::function
• std::function 是一个可调用对象包装器,是一个类模板,可以容纳除了类成员函数指针之外的所有可调用对象,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟它们的执行。
• 定义格式:std::function<函数类型>。
• std::function可以取代函数指针的作用,因为它可以延迟函数的执行,特别适合作为回调函数使用。它比普通函数指针更加的灵活和便利。
标准库std::function
二、std::bind
可将std::bind函数看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
std::bind将可调用对象与其参数一起进行绑定,绑定后的结果可以使用std::function保存。std::bind主要有以下两个作用:
• 将可调用对象和其参数绑定成一个仿函数;
• 只绑定部分参数,减少可调用对象传入的参数。
1. std::bind绑定普通函数
double my_divide (double x, double y) {return x/y;}
auto fn_half = std::bind (my_divide,_1,2);
std::cout << fn_half(10) << '\n'; // 5
• bind的第一个参数是函数名,普通函数做实参时,会隐式转换成函数指针。因此std::bind (my_divide,_1,2)等价于std::bind (&my_divide,_1,2);
• _1表示占位符,位于中,std::placeholders::_1;
2. std::bind绑定成员函数
struct Foo {
void print_sum(int n1, int n2)
{
std::cout << n1+n2 << '\n';
}
int data = 10;
};
int main()
{
Foo foo;
auto f = std::bind(&Foo::print_sum, &foo, 95, std::placeholders::_1);
f(5); // 100
}
• bind绑定类成员函数时,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址。
• 必须显示的指定&Foo::print_sum,因为编译器不会将对象的成员函数隐式转换成函数指针,所以必须在Foo::print_sum前添加&;
• 使用对象成员函数的指针时,必须要知道该指针属于哪个对象,因此第二个参数为对象的地址 &foo;
3. 绑定一个引用参数
a. 默认情况下,bind的那些不是占位符的参数被拷贝到bind返回的可调用对象中。但是,与lambda类似,有时对有些绑定的参数希望以引用的方式传递,或是要绑定参数的类型无法拷贝.
b. 所以直接拷贝函数对象?ref &()
#include <functional>
#include <sstream>
using namespace std::placeholders;
using namespace std;
ostream & print(ostream &os, const string& s, char c)
{
os << s << c;
return os;
}
int main()
{
vector<string> words{"helo", "world", "this", "is", "C++11"};
ostringstream os;
char c = ' ';
for_each(words.begin(), words.end(),
[&os, c](const string & s){os << s << c;} );
cout << os.str() << endl;
ostringstream os1;
// ostream不能拷贝,若希望传递给bind一个对象,
// 而不拷贝它,就必须使用标准库提供的ref函数
for_each(words.begin(), words.end(),
bind(print, ref(os1), _1, c));
cout << os1.str() << endl;
}
4.指向成员函数的指针
#include <iostream>
struct Foo {
int value;
void f() { std::cout << "f(" << this->value << ")\n"; }
void g() { std::cout << "g(" << this->value << ")\n"; }
};
void apply(Foo* foo1, Foo* foo2, void (Foo::*fun)()) {
(foo1->*fun)(); // call fun on the object foo1
(foo2->*fun)(); // call fun on the object foo2
}
int main() {
Foo foo1{1};
Foo foo2{2};
apply(&foo1, &foo2, &Foo::f);
apply(&foo1, &foo2, &Foo::g);
}
三、占位符-std::placehoders
namespace placeholders {
extern /* unspecified */ _1;
extern /* unspecified */ _2;
extern /* unspecified */ _3;
// ... }
其中_1, _2, _3是未指定的数字对象,用于function的bind中。 _1用于代替回调函数中的第一个参数, _2用于代替回调函数中的第二个参数,以此类推。
int main () {
using namespace std::placeholders; // introduces _1
std::cout << std::is_placeholder<decltype(_1)>::value << '\n';
std::cout << std::is_placeholder<decltype(_2)>::value << '\n';
std::cout << std::is_placeholder<int>::value << '\n';
return 0;
}
//输出:
1
2
0
std::is_bind_expression: 用来判断是否为bind表达式
int main () {
using namespace std::placeholders; // introduces _1
auto increase_int = std::bind (std::plus<int>(),_1,1);
std::cout << std::boolalpha;
std::cout << std::is_bind_expression<decltype(increase_int)>::value << '\n';
return 0;
}
//输出
true
————————————————