可调用对象
在 C++ 中存在 “可调用对象” 这个概念。可调用对象有如下几种定义:
(1)是一个函数指针
1 2 3 4 5 6 7 8
| int print(int a, double b) { cout << a << ", " << b << endl; return 0; }
int (*func)(int, double) = &print;
|
(2)是一个具有 operator()
成员函数的类对象(仿函数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> #include <string> using namespace std;
struct Test { void operator()(string msg) { cout << "msg: " << msg << endl; } };
int main() { Test t; t("hello world"); return 0; }
|
(3)是一个可以被转换为函数指针的类对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <string> using namespace std;
using func_ptr = void(*)(int, string);
struct Test { static void print(int a, string b) { cout << "name: " << b << ", age: " << a << endl; }
operator func_ptr() { return print; } };
int main() { Test t; t(25, "zhagsan");
return 0; }
|
(4)是一个类成员函数指针或者类成员指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <iostream> #include <string> using namespace std;
struct Test { void print(int a, string b) { cout << "name: " << b << ", age: " << a << endl; } int m_num; };
int main() { void (Test::*func_ptr)(int, string) = &Test::print; int Test::*obj_ptr = &Test::m_num;
Test t; (t.*func_ptr)(25, "zhangsan"); t.*obj_ptr = 1; cout << "number is: " << t.m_num << endl;
return 0; }
|
示例代码中满足条件的这些可调用对象对应的类型被称为可调用类型。C++ 中的可调用类型虽然具有比较统一的操作形式,但定义方式五花八门,试图使用统一的方式保存或者传递一个可调用对象时会非常繁琐。因此,C++11 通过提供 std::function
和 std::bind
统一了可调用对象的各种操作。
可调用对象包装器
std::function
是可调用对象的包装器。它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象。通过指定它的模板参数,可以用统一的方式处理函数/函数对象/函数指针,并且允许保存和延迟执行它们。
基本用法
std::function
必须要包含 functional
的头文件,可调用对象包装器使用语法如下:
1 2
| #include <functional> std::function<返回值类型(参数类型列表)> diy_name = 可调用对象;
|
示例代码中演示了可调用对象包装器的基本使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <iostream> #include <functional> using namespace std;
int add(int a, int b) { cout << a << " + " << b << " = " << a + b << endl; return a + b; }
class T1 { public: static int sub(int a, int b) { cout << a << " - " << b << " = " << a - b << endl; return a - b; } };
class T2 { public: int operator()(int a, int b) { cout << a << " * " << b << " = " << a * b << endl; return a * b; } };
int main() { function<int(int, int)> f1 = add; function<int(int, int)> f2 = T1::sub; T2 t; function<int(int, int)> f3 = t;
f1(9, 3); f2(9, 3); f3(9, 3);
return 0; }
|
测试代码得到结论:std::function
可以将可调用对象进行包装,得到一个统一的格式,包装完成后得到的对象相当于一个函数指针,和函数指针的使用方式相同,通过包装器对象就可以完成对包装函数的调用。
作为回调函数使用
回调函数本身就是通过函数指针实现的,使用对象包装器可以取代函数指针的作用,示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <iostream> #include <functional> using namespace std;
class A { public: A(const function<void()>& f) : callback(f) {}
void notify() { callback(); } private: function<void()> callback; };
class B { public: void operator()() { cout << "B operator()" << endl; } };
int main() { B b; A a(b); a.notify();
return 0; }
|
使用对象包装器 std::function
可以非常方便地将仿函数转换为一个函数指针,通过进行函数指针的传递,在其他函数的合适的位置就可以调用这个包装好的仿函数。另外,使用 std::function
作为函数的传入参数,可以将定义方式不相同的可调用对象进行统一的传递,增加了程序的灵活性。
绑定器
std::bind
将可调用对象与其参数一起进行绑定。绑定后的结果可以使用 std::function
进行保存,并延迟调用到我们需要的时候,它有两个作用:
(1)将可调用对象与其参数一起绑定成一个仿函数。
(2)将多元(参数个数为 n,n > 1
)可调用对象转换为一元或者 n - 1
元可调用对象,只绑定部分参数。
绑定器函数的使用,语法格式如下:
1 2 3 4
| auto f = std::bind(可调用对象地址, 绑定的参数/占位符);
auto f = std::bind(类函数/成员地址, 类实例对象地址, 绑定的参数/占位符);
|
关于绑定器的使用的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <iostream> #include <functional> using namespace std;
void callFunc(int x, const function<void(int)>& f) { if (x % 2 == 0) { f(x); } }
void output(int x) { cout << x << " "; }
void output_add(int x) { cout << x + 10 << " "; }
int main(void) { auto f1 = bind(output, placeholders::_1); for (int i = 0; i < 10; ++i) { callFunc(i, f1); } cout << endl;
auto f2 = bind(output_add, placeholders::_1); for (int i = 0; i < 10; ++i) { callFunc(i, f2); } cout << endl;
return 0; }
|
示例代码中使用了 std::bind
绑定器,在函数外部通过绑定不同的函数,控制了最后执行的结果。std::bind
绑定器返回的是一个仿函数类型,得到的返回值可以直接赋值给一个 std::function
,在使用的时候不需要关心绑定器的返回值类型,使用 auto
进行自动类型推导。
placeholders::_1
是一个占位符,代表这个位置将在函数调用时被传入的第一个参数所替代。同样还有其他的占位符 placeholders::_2
、placeholders::_3
、placeholders::_4
、placeholders::_5
等。
有了占位符之后,使得 std::bind
的使用变得非常灵活,示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream> #include <functional> using namespace std;
void output(int x, int y) { cout << x << " " << y << endl; }
int main() { bind(output, 1, 2)(); bind(output, placeholders::_1, 2)(10); bind(output, 2, placeholders::_1)(10);
bind(output, 2, placeholders::_2)(10, 20); bind(output, placeholders::_1, placeholders::_2)(10, 20); bind(output, placeholders::_2, placeholders::_1)(10, 20);
return 0; }
|
std::bind
可以直接绑定函数的所有参数,也可以只绑定部分参数。在绑定部分参数的时候,通过使用 std::placeholders
来决定空位参数将会属于调用发生时的第几个参数。
可调用对象包装器 std::function
是不能实现对类成员函数指针或者类成员指针的包装,但是通过绑定器 std::bind
配合之后,就可以完美的解决这个问题了,示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <iostream> #include <functional> using namespace std;
class Test { public: void output(int x, int y) { cout << "x: " << x << ", y: " << y << endl; } int m_number = 100; };
int main(void) { Test t; function<void(int, int)> f1 = bind(&Test::output, &t, placeholders::_1, placeholders::_2); function<int&(void)> f2 = bind(&Test::m_number, &t);
f1(520, 1314); f2() = 2333; cout << "t.m_number: " << t.m_number << endl;
return 0; }
|
在使用绑定器绑定类成员函数或者成员变量的时候,需要将它们所属的实例对象一起传递到绑定器函数内部。f1 的类型是 function<void(int, int)>
,通过使用 std::bind
将 Test 的成员函数 output 的地址和对象 t 绑定,并转化为一个仿函数并存储到对象 f1
中。
使用绑定器绑定的类成员变量 m_number
得到的仿函数被存储到了类型为 function<int&(void)>
的包装器对象 f2
中,并且可以在需要的时候修改这个成员。其中 int
是绑定的类成员的类型,并且允许修改绑定的变量,因此需要指定为变量的引用;由于没有参数,因此参数列表指定为 void
。
示例代码中使用 function
包装器保存了 bind
返回的仿函数。如果不知道包装器的模板类型如何指定,可以直接使用 auto
进行类型的自动推导,这样使用起来更容易一些。
参考资料
https://subingwen.cn/cpp/bind/