淺談c++11閉包的實(shí)現(xiàn)
什么是閉包
一個(gè)函數(shù),帶上了一個(gè)狀態(tài),就變成了閉包了。那什么叫 “帶上狀態(tài)” 呢? 意思是這個(gè)閉包有屬于自己的變量,這些個(gè)變量的值是創(chuàng)建閉包的時(shí)候設(shè)置的,并在調(diào)用閉包的時(shí)候,可以訪問(wèn)這些變量。
函數(shù)是代碼,狀態(tài)是一組變量,將代碼和一組變量捆綁 (bind) ,就形成了閉包。
閉包的狀態(tài)捆綁,必須發(fā)生在運(yùn)行時(shí)。
仿函數(shù):重載 operator()
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> class MyFunctor { public: MyFunctor(int temp): round(temp) {} int operator()(int temp) {return temp + round; } private: int round; }; void mytest() { int round = 2; MyFunctor f(round); std::cout << "result: " << f(1) << std::endl; // operator()(int temp) return; } int main() { mytest(); system("pause"); return 0; }
std::bind綁定器
在C++中,可調(diào)用實(shí)體主要包括:函數(shù)、函數(shù)指針、函數(shù)引用、可以隱式轉(zhuǎn)換為函數(shù)指定的對(duì)象,或者實(shí)現(xiàn)了opetator()的對(duì)象。
C++11中,新增加了一個(gè)std::function類模板,它是對(duì)C++中現(xiàn)有的可調(diào)用實(shí)體的一種類型安全的包裹。通過(guò)指定它的模板參數(shù),它可以用統(tǒng)一的方式處理函數(shù)、函數(shù)對(duì)象、函數(shù)指針,并允許保存和延遲執(zhí)行它們。
std::function對(duì)象最大的用處就是在實(shí)現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來(lái)檢查相等或者不相等,但是可以與NULL或者nullptr進(jìn)行比較。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <functional> #include <vector> #include <map> void func(void) {// 普通全局函數(shù) std::cout << __FUNCTION__ << std::endl; } class Foo { public: static int foo_func(int a) {// 類中的靜態(tài)函數(shù) std::cout << __FUNCTION__ << "(" << a << ")->: "; return a; } }; class Bar { public: int operator ()(int a) {// 仿函數(shù) std::cout << __FUNCTION__ << "(" << a << ")->: "; return a; } }; void mytest() { // std::function對(duì)象最大的用處就是在實(shí)現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來(lái)檢查相等或者不相等,但是可以與NULL或者nullptr進(jìn)行比較。 // 綁定一個(gè)普通函數(shù) std::function< void(void) > f1 = func; f1(); // 綁定類中的靜態(tài)函數(shù) std::function<int(int)> f2 = Foo::foo_func; std::cout << f2(11) << std::endl; // 綁定一個(gè)仿函數(shù) Bar obj; std::function<int(int)> f3 = obj; std::cout << f3(222) << std::endl; /* 運(yùn)行結(jié)果: func Foo::foo_func(11)->: 11 Bar::operator ()(222)->: 222 */ return; } int main() { mytest(); system("pause"); return 0; }
std::bind
std::bind是這樣一種機(jī)制,它可以預(yù)先把指定可調(diào)用實(shí)體的某些參數(shù)綁定到已有的變量,產(chǎn)生一個(gè)新的可調(diào)用實(shí)體,這種機(jī)制在回調(diào)函數(shù)的使用過(guò)程中也頗為有用。
C++98中,有兩個(gè)函數(shù)bind1st和bind2nd,它們分別可以用來(lái)綁定functor的第一個(gè)和第二個(gè)參數(shù),它們都是只可以綁定一個(gè)參數(shù),各種限制,使得bind1st和bind2nd的可用性大大降低。
在C++11中,提供了std::bind,它綁定的參數(shù)的個(gè)數(shù)不受限制,綁定的具體哪些參數(shù)也不受限制,由用戶指定,這個(gè)bind才是真正意義上的綁定。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <functional> #include <vector> #include <map> void func(int x, int y) { std::cout << x << " " << y << std::endl; } void mytest() { std::bind(func, 1, 2)(); std::bind(func, std::placeholders::_1, 2)(1); func(1, 2); // std::placeholders 表示的是占位符 // std::placeholders::_1是一個(gè)占位符,代表這個(gè)位置將在函數(shù)調(diào)用時(shí),被傳入的第一個(gè)參數(shù)所替代。 std::bind(func, 2, std::placeholders::_1)(1); std::bind(func, 2, std::placeholders::_2)(1, 2); std::bind(func, std::placeholders::_1, std::placeholders::_2)(1, 2); std::bind(func, std::placeholders::_3, std::placeholders::_2)(1, 2, 3); //std::bind(func, 2, std::placeholders::_2)(1); // err, 調(diào)用時(shí)沒(méi)有第二個(gè)參數(shù) return; } int main() { mytest(); system("pause"); return 0; }
std::bind和std::function配合使用
通過(guò)std::bind和std::function配合使用,所有的可調(diào)用對(duì)象均有了統(tǒng)一的操作方法
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <functional> #include <vector> #include <map> class Test { public: int i; // 非靜態(tài)成員變量 void func(int x, int y) { // 非靜態(tài)成員函數(shù) std::cout << x << " " << y << std::endl; } }; void mytest() { Test obj; // 創(chuàng)建對(duì)象 // 綁定非靜態(tài)成員函數(shù) std::function<void(int, int)> f1 = std::bind(&Test::func, &obj, std::placeholders::_1, std::placeholders::_2); f1(1, 2); // 輸出: 1 2 obj.i = 10; // 綁定非靜態(tài)成員變量 std::function<int &()> f2 = std::bind(&Test::i, &obj); f2() = 123; // obj.i = 123; std::cout << "obj.i: " << obj.i << std::endl; return; } int main() { mytest(); system("pause"); return 0; }
以上就是淺談c++11閉包的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于c++11閉包的實(shí)現(xiàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12C語(yǔ)言使用rand函數(shù)生成隨機(jī)數(shù)
這篇文章介紹了C語(yǔ)言使用rand函數(shù)生成隨機(jī)數(shù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12C語(yǔ)言實(shí)現(xiàn)繪制可愛(ài)的橘子鐘表
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)繪制可愛(ài)的橘子鐘表,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2022-12-12c++字符串char[]數(shù)組分割split問(wèn)題
這篇文章主要介紹了c++字符串char[]數(shù)組分割split問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09c++中struct和class的區(qū)別小結(jié)
在C++中,class和struct都是用于定義自定義數(shù)據(jù)類型的關(guān)鍵字,本文主要介紹了c++中struct和class的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08C++ vector容器 find erase的使用操作:查找并刪除指定元素
這篇文章主要介紹了C++ vector容器 find erase的使用操作:查找并刪除指定元素,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05