std::function簡(jiǎn)介std::function是一個(gè)函數(shù)包裝器,該函數(shù)包裝器模板能包裝任何類型的可調(diào)用實(shí)體,如普通函數(shù),函數(shù)對(duì)象,lamda表達(dá)式等。包裝器可拷貝,移動(dòng)等,并且包裝器類型僅僅依賴于調(diào)用特征,而不依賴于可調(diào)用元素自身的類型。std::function是C++11的新特性,包含在頭文件<functional>中。 一個(gè)std::function類型對(duì)象實(shí)例可以包裝下列這幾種可調(diào)用實(shí)體:函數(shù)、函數(shù)指針、成員函數(shù)、靜態(tài)函數(shù)、lamda表達(dá)式和函數(shù)對(duì)象。std::function對(duì)象實(shí)例可被拷貝和移動(dòng),并且可以使用指定的調(diào)用特征來(lái)直接調(diào)用目標(biāo)元素。當(dāng)std::function對(duì)象實(shí)例未包含任何實(shí)際可調(diào)用實(shí)體時(shí),調(diào)用該std::function對(duì)象實(shí)例將拋出std::bad_function_call異常。 std::function實(shí)戰(zhàn)std::function模板類聲明 template<class _Rp, class ..._ArgTypes>class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)> : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>, public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>{ ... } std::function模板類成員函數(shù)聲明
從成員函數(shù)里我們知道std::function對(duì)象實(shí)例不允許進(jìn)行==和!=比較操作,std::function模板類實(shí)例最終調(diào)用成員函數(shù)_Rp operator()(_ArgTypes...) const進(jìn)而調(diào)用包裝的調(diào)用實(shí)體。 1、std::function包裝函數(shù)指針 定義一個(gè)std::function<int(int)>對(duì)象實(shí)例 std::function<int(int)> callback; std::function對(duì)象實(shí)例包裝函數(shù)指針
2、std::function包裝函數(shù) int fun1(int a){ return a;}int main(int argc, char *argv[]){ std::cout << 'Hello world' << std::endl; callback = fun1; //std::function包裝函數(shù) std::cout << callback(42) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體 return 0;} 3、std::function包裝模板函數(shù)
4、std::function包裝函數(shù)對(duì)象 struct add{ int operator()(int x){ return x + 9; }};int main(int argc, char *argv[]){ std::cout << 'Hello world' << std::endl; callback = add(); //std::function包裝對(duì)象函數(shù) std::cout << callback(2) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體 return 0;} 5、std::function包裝lamda表達(dá)式
6、std::function包裝模板對(duì)象函數(shù) template <typename T>struct sub{ T operator()(T a){ return a - 8; }};int main(int argc, char *argv[]){ std::cout << 'Hello world' << std::endl; callback = sub<int>(); //std::function包裝模板對(duì)象函數(shù) std::cout << callback(2) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體 return 0;} 7、std::function包裝模板對(duì)象靜態(tài)函數(shù)
8、std::function包裝對(duì)象靜態(tài)函數(shù) struct foo1{ static int foo(int a){ return a * 3; }};int main(int argc, char *argv[]){ std::cout << 'Hello world' << std::endl; callback = foo1::foo; //std::function包裝對(duì)象靜態(tài)函數(shù) std::cout << callback(5) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體 return 0;} 9、std::function包裝類成員函數(shù)
這里我們用到了std::bind,C++11中std::bind函數(shù)的意義就如字面上的意思一樣,用來(lái)綁定函數(shù)調(diào)用的某些參數(shù)。std::bind的思想其實(shí)是一種延遲計(jì)算的思想,將可調(diào)用對(duì)象保存起來(lái),然后在需要的時(shí)候再調(diào)用。而且這種綁定是非常靈活的,不論是普通函數(shù)還是函數(shù)對(duì)象還是成員函數(shù)都可以綁定,而且其參數(shù)可以支持占位符。 這里的std::placeholders::_1是一個(gè)占位符,且綁定第一個(gè)參數(shù),若可調(diào)用實(shí)體有2個(gè)形參,那么綁定第二個(gè)參數(shù)的占位符是std::placeholders::_2。 10、std::function包裝模板類成員函數(shù) template <typename T>struct foo4{ T foo(T a){ return a * 6; }};int main(int argc, char *argv[]){ std::cout << 'Hello world' << std::endl; foo4<int> test_foo2; callback = std::bind(&foo4<int>::foo, test_foo2, std::placeholders::_1); //std::function包裝模板類成員函數(shù) std::cout << callback(7) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體 return 0;} 11、std::function拷貝、移動(dòng)
|
|
來(lái)自: 菌心說 > 《編程+、計(jì)算機(jī)、信息技術(shù)》