C++11中std::function與std::bind的用法實例
關于std::function 的用法:
其實就可以理解成函數(shù)指針
1. 保存自由函數(shù)
void printA(int a) { cout<<a<<endl; } std::function<void(int a)> func; func = printA; func(2);
保存lambda表達式
std::function<void()> func_1 = [](){cout<<"hello world"<<endl;}; func_1();
保存成員函數(shù)
struct Foo { Foo(int num) : num_(num) {} void print_add(int i) const { cout << num_+i << '\n'; } int num_; }; // 保存成員函數(shù) 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 的用法:
看一系列的文字,不如看一段代碼理解的快
#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); //表示綁定函數(shù) fun 的第一,二,三個參數(shù)值為: 1 2 3 f1(); //print:1 2 3 auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3); //表示綁定函數(shù) fun 的第三個參數(shù)為 3,而fun 的第一,二個參數(shù)分別有調用 f2 的第一,二個參數(shù)指定 f2(1,2);//print:1 2 3 auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3); //表示綁定函數(shù) fun 的第三個參數(shù)為 3,而fun 的第一,二個參數(shù)分別有調用 f3 的第二,一個參數(shù)指定 //注意: f2 和 f3 的區(qū)別。 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對于不事先綁定的參數(shù),通過std::placeholders傳遞的參數(shù)是通過引用傳遞的 cout<<n<<endl;//print:2 說明:bind對于預先綁定的函數(shù)參數(shù)是通過值傳遞的 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可以指向類成員函數(shù)和函數(shù)簽名不一樣的函數(shù),其實,這兩種函數(shù)都是一樣的,因為類成員函數(shù)都有一個默認的參數(shù),this,作為第一個參數(shù),這就導致了類成員函數(shù)不能直接賦值給std::function,這時候我們就需要std::bind了,簡言之,std::bind的作用就是轉換函數(shù)簽名,將缺少的參數(shù)補上,將多了的參數(shù)去掉,甚至還可以交換原來函數(shù)參數(shù)的位置,具體用法如下列代碼所示:
typedef std::function<void (int)> PrintFinFunction; void print(const char *text, PrintFinFunction callback) { printf("%s\n", text); if (callback) callback(0); } // 類成員函數(shù) class Test { public: void printFinCallbackInter(int res) { cout << "Class Inter callback" << endl; } }; // 函數(shù)簽名不一樣的函數(shù) 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); //函數(shù)模板只有一個參數(shù),這里需要補充this參數(shù) auto callback6 = std::bind(&printFinCallback2, std::placeholders::_1, 100); print("test 6", callback6); //這里需要補充第二個參數(shù)
從上面的代碼中可以看到,std::bind的用法就是第一個參數(shù)是要被指向的函數(shù)的地址,為了區(qū)分,這里std::bind語句的左值函數(shù)為原函數(shù),右值函數(shù)為新函數(shù),那么std::bind方法從第二個參數(shù)起,都是新函數(shù)所需要的參數(shù),缺一不可,而我們可以使用std::placeholders::_1或std::placeholders::_2等等來使用原函數(shù)的參數(shù),_1就是原函數(shù)的第一個參數(shù),如此類推。
值得注意的有兩點:
一旦bind補充了缺失的參數(shù),那么以后每次調用這個function時,那些原本缺失的參數(shù)都是一樣的,舉個栗子,上面代碼中callback6,我們每次調用它的時候,第二個參數(shù)都只會是100。
正因為第一點,所以假如我們是在iOS程序中使用std::bind傳入一個缺失參數(shù),那么我們轉化后的那個function會持有那些缺失參數(shù),這里我們需要防止出現(xiàn)循環(huán)引用導致內存泄漏。
總結
到此這篇關于C++11中std::function與std::bind用法的文章就介紹到這了,更多相關C++11 std::function與std::bind內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VisualStudio2019配置OpenCV4.5.0的方法示例
這篇文章主要介紹了VisualStudio2019配置OpenCV4.5.0的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03