淺談c++11閉包的實現(xiàn)
什么是閉包
一個函數(shù),帶上了一個狀態(tài),就變成了閉包了。那什么叫 “帶上狀態(tài)” 呢? 意思是這個閉包有屬于自己的變量,這些個變量的值是創(chuàng)建閉包的時候設(shè)置的,并在調(diào)用閉包的時候,可以訪問這些變量。
函數(shù)是代碼,狀態(tài)是一組變量,將代碼和一組變量捆綁 (bind) ,就形成了閉包。
閉包的狀態(tài)捆綁,必須發(fā)生在運(yùn)行時。
仿函數(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ù)引用、可以隱式轉(zhuǎn)換為函數(shù)指定的對象,或者實現(xiàn)了opetator()的對象。
C++11中,新增加了一個std::function類模板,它是對C++中現(xiàn)有的可調(diào)用實體的一種類型安全的包裹。通過指定它的模板參數(shù),它可以用統(tǒng)一的方式處理函數(shù)、函數(shù)對象、函數(shù)指針,并允許保存和延遲執(zhí)行它們。
std::function對象最大的用處就是在實現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來檢查相等或者不相等,但是可以與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對象最大的用處就是在實現(xiàn)函數(shù)回調(diào),使用者需要注意,它不能被用來檢查相等或者不相等,但是可以與NULL或者nullptr進(jìn)行比較。
// 綁定一個普通函數(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;
// 綁定一個仿函數(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ù)綁定到已有的變量,產(chǎn)生一個新的可調(diào)用實體,這種機(jī)制在回調(diào)函數(shù)的使用過程中也頗為有用。
C++98中,有兩個函數(shù)bind1st和bind2nd,它們分別可以用來綁定functor的第一個和第二個參數(shù),它們都是只可以綁定一個參數(shù),各種限制,使得bind1st和bind2nd的可用性大大降低。
在C++11中,提供了std::bind,它綁定的參數(shù)的個數(shù)不受限制,綁定的具體哪些參數(shù)也不受限制,由用戶指定,這個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是一個占位符,代表這個位置將在函數(shù)調(diào)用時,被傳入的第一個參數(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ù)
return;
}
int main()
{
mytest();
system("pause");
return 0;
}
std::bind和std::function配合使用
通過std::bind和std::function配合使用,所有的可調(diào)用對象均有了統(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)建對象
// 綁定非靜態(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閉包的實現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于c++11閉包的實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言實現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12
C語言使用rand函數(shù)生成隨機(jī)數(shù)
這篇文章介紹了C語言使用rand函數(shù)生成隨機(jī)數(shù)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
c++中struct和class的區(qū)別小結(jié)
在C++中,class和struct都是用于定義自定義數(shù)據(jù)類型的關(guān)鍵字,本文主要介紹了c++中struct和class的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-08-08
C++ vector容器 find erase的使用操作:查找并刪除指定元素
這篇文章主要介紹了C++ vector容器 find erase的使用操作:查找并刪除指定元素,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05

