关于std::function 的用法:
其实就可以理解成函数指针
1. 保存自由函数
1
2
3
4
5
6
7
8
|
void printA( int a) { cout<<a<<endl; } std::function< void ( int a)> func; func = printA; func(2); |
保存lambda表达式
1
2
|
std::function< void ()> func_1 = [](){cout<< "hello world" <<endl;}; func_1(); |
保存成员函数
1
2
3
4
5
6
7
8
9
10
|
struct Foo { Foo( int num) : num_(num) {} void print_add( int i) const { cout << num_+i << '\n' ; } int num_; }; // 保存成员函数 std::function< void ( const Foo&, int )> f_add_display = &Foo::print_add; Foo foo(2); f_add_display(foo, 1); |
在实际使用中都用 auto 关键字来代替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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include <iostream> using namespace std; class A { public : void fun_3( int k, int m) { cout<<k<< " " <<m<<endl; } }; void fun( int x, int y, int z) { cout<<x<< " " <<y<< " " <<z<<endl; } void fun_2( int &a, int &b) { a++; b++; cout<<a<< " " <<b<<endl; } int main( int argc, const char * argv[]) { auto f1 = std::bind(fun,1,2,3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3 f1(); //print:1 2 3 auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3); //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别有调用 f2 的第一,二个参数指定 f2(1,2); //print:1 2 3 auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3); //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别有调用 f3 的第二,一个参数指定 //注意: f2 和 f3 的区别。 f3(1,2); //print:2 1 3 int n = 2; int m = 3; auto f4 = std::bind(fun_2, n,placeholders::_1); f4(m); //print:3 4 cout<<m<<endl; //print:4 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的 cout<<n<<endl; //print:2 说明:bind对于预先绑定的函数参数是通过值传递的 A a; auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2); f5(10,20); //print:10 20 std::function< void ( int , int )> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); fc(10,20); //print:10 20 return 0; } |
附:std::function与std::bind双剑合璧
std::function可以指向类成员函数和函数签名不一样的函数,其实,这两种函数都是一样的,因为类成员函数都有一个默认的参数,this,作为第一个参数,这就导致了类成员函数不能直接赋值给std::function,这时候我们就需要std::bind了,简言之,std::bind的作用就是转换函数签名,将缺少的参数补上,将多了的参数去掉,甚至还可以交换原来函数参数的位置,具体用法如下列代码所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
typedef std::function< void ( int )> PrintFinFunction; void print( const char *text, PrintFinFunction callback) { printf ( "%s\n" , text); if (callback) callback(0); } // 类成员函数 class Test { public : void printFinCallbackInter( int res) { cout << "Class Inter callback" << endl; } }; // 函数签名不一样的函数 void printFinCallback2( int res1, int res2) { cout << "Different callback " << res1 << " " << res2 << endl; } Test testObj; auto callback5 = std::bind(&Test::printFinCallbackInter, testObj, std::placeholders::_1); print( "test 5" , callback5); //函数模板只有一个参数,这里需要补充this参数 auto callback6 = std::bind(&printFinCallback2, std::placeholders::_1, 100); print( "test 6" , callback6); //这里需要补充第二个参数 |
从上面的代码中可以看到,std::bind的用法就是第一个参数是要被指向的函数的地址,为了区分,这里std::bind语句的左值函数为原函数,右值函数为新函数,那么std::bind方法从第二个参数起,都是新函数所需要的参数,缺一不可,而我们可以使用std::placeholders::_1或std::placeholders::_2等等来使用原函数的参数,_1就是原函数的第一个参数,如此类推。
值得注意的有两点:
一旦bind补充了缺失的参数,那么以后每次调用这个function时,那些原本缺失的参数都是一样的,举个栗子,上面代码中callback6,我们每次调用它的时候,第二个参数都只会是100。
正因为第一点,所以假如我们是在iOS程序中使用std::bind传入一个缺失参数,那么我们转化后的那个function会持有那些缺失参数,这里我们需要防止出现循环引用导致内存泄漏。
总结
到此这篇关于C++11中std::function与std::bind用法的文章就介绍到这了,更多相关C++11 std::function与std::bind内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/liukang325/article/details/53668046