C++11系列學(xué)習(xí)之可調(diào)用對象包裝器和綁定器
舊標準的缺陷
學(xué)習(xí)新標準的語法之前,先來聊聊舊標準存在的缺陷,這樣就可以理解為什么要在C++11中存在std::funtion和std::bind了,這就是所謂——知其然,并知其所以然
繁雜的調(diào)用對象
C++中可以調(diào)用的對象有很多,包括但不限于以下:
- 函數(shù)指針
- 仿函數(shù)(重載了"( )" 運算符)
- 可被轉(zhuǎn)換為函數(shù)指針的類對象
- 類成員函數(shù)
問題浮出水面
這些調(diào)用對象在使用的時候具有比較統(tǒng)一的時候,但是定義的方式卻是五花八門。因此,C++11中為了改善這個問題,提供了std::function和std::bind來解決
std::function
std::function是可調(diào)用對象的包裝器,本質(zhì)上是一個類模板,可容納除了類成員(函數(shù))指針之外的所有可調(diào)用對象
小試牛刀
#include<iostream>
#include<functional>
class A
{
std::function<void()> callback_;
public:
A(const std::function<void()>& f) : callback_(f){}
void notify(void){
callback_(); //調(diào)用這個回調(diào)對象
}
};
class Foo{
public:
void operator()(void){
std::cout << __FUNCTION__ << std::endl;
}
};
int main(void)
{
Foo foo;
A aa(foo);
aa.notify(); //調(diào)用這個回調(diào)
return 0;
}
//輸出
//operator()std::function因為有著保存函數(shù)并可以延遲執(zhí)行的特性,因此非常適合作為回調(diào)函數(shù)來使用
std::bind
std::bind用來將可調(diào)用對象與其參數(shù)一起進行綁定,綁定后的結(jié)果可以使用std::function進行保存,并延遲調(diào)用。
作用
- 將可調(diào)用對象與其參數(shù)一起綁定成一個仿函數(shù)
- 將N元可調(diào)用對象轉(zhuǎn)換成一元或N-1元可調(diào)用對象,即只綁定部分參數(shù)
占位符
std::bind可以將函數(shù)及其參數(shù)進行綁定,那么參數(shù)的位置與傳入的位置相關(guān),因此有了占位符的概念,例如std::placeholders::_1是第一個參數(shù)的占位符,當(dāng)函數(shù)傳入第一個參數(shù)的時候,就會把其進行替換,同理std::palceholders::_2是第二個占位符。
#include<iostream>
#include<functional>
void output(int x, int y){
std::cout << x << " " << y << std::endl;
}
int main(){
std::bind(output, 1, 2)(); //輸出:1 2
std::bind(output, std::placeholders::_1)(1); //輸出:1
std::bind(output, 2, std::placeholders::_2)(1,2); //輸出:2 2
std::bind(output, std::placeholders::_2, std::placeholders::_1)(1, 2); //輸出:2 1
}高級用法
可以使用std::bind進行多個函數(shù)的組合,例如找出集合中大于5小于10的元素個數(shù):
#include<iostream> #include<functional> using std::placeholders::_1; auto f = std::bind(std::logical_and<bool>(), std::bind(std::greater<int>(), _1, 5),std::bind(std::less_equal<int>(), _1, 10)); //將兩個函數(shù)參數(shù)綁定并進行組合 int count = std::count_if(coll.begin(), coll.end(), f); //使用
配合使用
std::bind和std::function配合使用非常的強大
#include<iostream>
#include<funcational>
class A{
public:
int i_ = 0;
void output(int x, int y){
std:cout << x << " " << y << std::endl;
}
};
int main(void){
A a;
std::function<void(int, int)> fr = std::bind(&A::output, &a, std::placeholders::_1,std::palceholders::_2); //注意
fr(1, 2); //輸出:1 2
std::function<int&(void)> fr_i = std::bind(&A::i, &a);
fr_i() = 123;
std::cout << a.i_ << std::endl; //輸出:123
}可以看到上面std::bind在綁定類的成員函數(shù)的時候,需要把&a也傳進去,這是因為成員函數(shù)編譯器在使用的時候會自動傳入一個this指針,所以我們綁定的時候需要額外傳一個對象的地址
到此這篇關(guān)于C++11系列學(xué)習(xí)之可調(diào)用對象包裝器和綁定器的文章就介紹到這了,更多相關(guān)C++11包裝器和綁定器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)坦克大戰(zhàn)小游戲EGE圖形界面
這篇文章主要為大家詳細介紹了C++實現(xiàn)坦克大戰(zhàn)小游戲EGE圖形界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
二維指針動態(tài)分配內(nèi)存連續(xù)問題深入分析
當(dāng)我們定義一個二維指針時,如果需要存儲相應(yīng)的數(shù)據(jù),就需要我們動態(tài)的分配內(nèi)存,這時,有一點是需要注意的,分配內(nèi)存的方法不同,內(nèi)存的連續(xù)性也是不相同的2013-07-07
C++ 中字符串操作--寬窄字符轉(zhuǎn)換的實例詳解
這篇文章主要介紹了C++ 中字符串操作--寬窄字符轉(zhuǎn)換的實例詳解的相關(guān)資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能更,需要的朋友可以參考下2017-09-09
Visual Studio 2019 DLL動態(tài)庫連接實例(圖文教程)
這篇文章主要介紹了Visual Studio 2019 DLL動態(tài)庫連接實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
關(guān)于C++中的友元函數(shù)的一些總結(jié)
以下是對C++中的友元函數(shù)進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下2013-09-09

