0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

std::function簡介及模板類聲明

5jek_harmonyos ? 來源:編程學習總站 ? 作者:寫代碼的牛頓 ? 2021-07-28 15:30 ? 次閱讀

01

std::function簡介

std::function是一個函數(shù)包裝器,該函數(shù)包裝器模板能包裝任何類型的可調(diào)用實體,如普通函數(shù),函數(shù)對象,lamda表達式等。包裝器可拷貝,移動等,并且包裝器類型僅僅依賴于調(diào)用特征,而不依賴于可調(diào)用元素自身的類型。

std::function是C++11的新特性,包含在頭文件《functional》中。一個std::function類型對象實例可以包裝下列這幾種可調(diào)用實體:函數(shù)、函數(shù)指針、成員函數(shù)、靜態(tài)函數(shù)、lamda表達式和函數(shù)對象。

std::function對象實例可被拷貝和移動,并且可以使用指定的調(diào)用特征來直接調(diào)用目標元素。當std::function對象實例未包含任何實際可調(diào)用實體時,調(diào)用該std::function對象實例將拋出std::bad_function_call異常。02

std::function實戰(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ù)聲明

typedef _Rp result_type; // construct/copy/destroy: _LIBCPP_INLINE_VISIBILITY function() _NOEXCEPT { } _LIBCPP_INLINE_VISIBILITY function(nullptr_t) _NOEXCEPT {} function(const function&); function(function&&) _NOEXCEPT; template《class _Fp, class = _EnableIfCallable《_Fp》》 function(_Fp); #if _LIBCPP_STD_VER 《= 14 template《class _Alloc》 _LIBCPP_INLINE_VISIBILITY function(allocator_arg_t, const _Alloc&) _NOEXCEPT {} template《class _Alloc》 _LIBCPP_INLINE_VISIBILITY function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {} template《class _Alloc》 function(allocator_arg_t, const _Alloc&, const function&); template《class _Alloc》 function(allocator_arg_t, const _Alloc&, function&&); template《class _Fp, class _Alloc, class = _EnableIfCallable《_Fp》》 function(allocator_arg_t, const _Alloc& __a, _Fp __f); #endif function& operator=(const function&); function& operator=(function&&) _NOEXCEPT; function& operator=(nullptr_t) _NOEXCEPT; template《class _Fp, class = _EnableIfCallable《_Fp》》 function& operator=(_Fp&&); ~function(); // function modifiers: void swap(function&) _NOEXCEPT; #if _LIBCPP_STD_VER 《= 14 template《class _Fp, class _Alloc》 _LIBCPP_INLINE_VISIBILITY void assign(_Fp&& __f, const _Alloc& __a) {function(allocator_arg, __a, _VSTD::forward《_Fp》(__f)).swap(*this);} #endif // function capacity: _LIBCPP_INLINE_VISIBILITY _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return static_cast《bool》(__f_); } // deleted overloads close possible hole in the type system template《class _R2, class.。。 _ArgTypes2》 bool operator==(const function《_R2(_ArgTypes2.。。)》&) const = delete; template《class _R2, class.。。 _ArgTypes2》 bool operator!=(const function《_R2(_ArgTypes2.。。)》&) const = delete; public: // function invocation: _Rp operator()(_ArgTypes.。。) const; #ifndef _LIBCPP_NO_RTTI // function target access: const std::type_info& target_type() const _NOEXCEPT; template 《typename _Tp》 _Tp* target() _NOEXCEPT; template 《typename _Tp》 const _Tp* target() const _NOEXCEPT; #endif // _LIBCPP_NO_RTTI從成員函數(shù)里我們知道std::function對象實例不允許進行==和!=比較操作,std::function模板類實例最終調(diào)用成員函數(shù)_Rp operator()(_ArgTypes.。。) const進而調(diào)用包裝的調(diào)用實體。1、std::function包裝函數(shù)指針定義一個std::function《int(int)》對象實例

std::function《int(int)》 callback;std::function對象實例包裝函數(shù)指針

int (*fun_ptr)(int); int fun1(int a){ return a; } int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; fun_ptr = fun1; //函數(shù)指針fun_ptr指向fun1函數(shù) callback = fun_ptr; //std::function對象包裝函數(shù)指針 std::cout 《《 callback(10) 《《 std::endl; //std::function對象實例調(diào)用包裝的實體 return 0; }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對象實例調(diào)用包裝的調(diào)用實體 return 0; }3、std::function包裝模板函數(shù)

template《typename T》 T fun2(T a){ return a + 2; } int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = fun2《int》; //std::function包裝模板函數(shù) std::cout 《《 callback(10) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }4、std::function包裝函數(shù)對象

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包裝對象函數(shù) std::cout 《《 callback(2) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }5、std::function包裝lamda表達式

int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; auto fun3 = [](int a) {return a * 2;}; //lamda表達式 callback = fun3; //std::function包裝lamda表達式 std::cout 《《 callback(9) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

6、std::function包裝模板對象函數(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包裝模板對象函數(shù) std::cout 《《 callback(2) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

7、std::function包裝模板對象靜態(tài)函數(shù)

template 《typename T》 struct foo2{ static T foo(T a){ return a * 4; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; callback = foo2《int》::foo; //std::function包裝模板對象靜態(tài)函數(shù) std::cout 《《 callback(3) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

8、std::function包裝對象靜態(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包裝對象靜態(tài)函數(shù) std::cout 《《 callback(5) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

9、std::function包裝類成員函數(shù)

struct foo3{ int foo(int a){ return a * a; } }; int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; foo3 test_foo1; callback = std::bind(&foo3::foo, test_foo1, std::_1); //std::function包裝類成員函數(shù) std::cout 《《 callback(9) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

這里我們用到了std::bind,C++11中std::bind函數(shù)的意義就如字面上的意思一樣,用來綁定函數(shù)調(diào)用的某些參數(shù)。std::bind的思想其實是一種延遲計算的思想,將可調(diào)用對象保存起來,然后在需要的時候再調(diào)用。而且這種綁定是非常靈活的,不論是普通函數(shù)還是函數(shù)對象還是成員函數(shù)都可以綁定,而且其參數(shù)可以支持占位符。

這里的std::_1是一個占位符,且綁定第一個參數(shù),若可調(diào)用實體有2個形參,那么綁定第二個參數(shù)的占位符是std::_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::_1); //std::function包裝模板類成員函數(shù) std::cout 《《 callback(7) 《《 std::endl; //std::function對象實例調(diào)用包裝的調(diào)用實體 return 0; }

11、std::function拷貝、移動

int main(int argc, char *argv[]){ std::cout 《《 “Hello world” 《《 std::endl; std::function《int(int)》 callback2 = callback; //拷貝賦值運算符 std::cout 《《 callback2(7) 《《 std::endl; std::function《int(int)》&& callback3 = std::move(callback); //移動賦值運算符 std::cout 《《 callback3(7) 《《 std::endl; std::cout 《《 callback(7) 《《 std::endl; std::function《int(int)》 callback4(callback); //拷貝 std::cout 《《 callback4(7) 《《 std::endl; return 0; }

編輯:jq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • STD
    STD
    +關(guān)注

    關(guān)注

    0

    文章

    36

    瀏覽量

    14314

原文標題:C++ std::function詳解與實戰(zhàn)

文章出處:【微信號:harmonyos_developer,微信公眾號:harmonyos_developer】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦

    深入解析MIL-STD-1553B模塊卡

    MIL-STD-1553B模塊
    發(fā)表于 09-06 11:43 ?0次下載

    如何對MIL-STD-1553B進行選型

    MIL-STD-1553B產(chǎn)品選型是一個復(fù)雜而細致的過程,?需要綜合考慮多個因素以確保所選產(chǎn)品能夠滿足特定應(yīng)用場景的需求。一、?引言MIL-STD-1553B作為一種廣泛應(yīng)用于航空航天領(lǐng)域的數(shù)據(jù)總線
    的頭像 發(fā)表于 09-05 17:37 ?193次閱讀
    如何對MIL-<b class='flag-5'>STD</b>-1553B進行選型

    基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)庫案例:SafeQueue

    1、程序簡介該程序是基于OpenHarmony的C++公共基礎(chǔ)庫的線程安全隊列:SafeQueue。線程安全隊列,是在dequeue的基礎(chǔ)上封裝std::lock_guard,以此實現(xiàn)線程的相關(guān)
    的頭像 發(fā)表于 08-30 12:41 ?138次閱讀
    基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)<b class='flag-5'>類</b>庫案例:SafeQueue

    基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)庫案例:SafeStack

    1、程序簡介該程序是基于OpenHarmony的C++公共基礎(chǔ)庫的線程安全隊列:SafeQueue。線程安全隊列,是在dequeue的基礎(chǔ)上封裝std::lock_guard,以此實現(xiàn)線程的相關(guān)
    的頭像 發(fā)表于 08-30 12:41 ?147次閱讀
    基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)<b class='flag-5'>類</b>庫案例:SafeStack

    verilog task和function區(qū)別

    verilog中的task和function都是用于實現(xiàn)模塊中的可重復(fù)的功能,并且可以接收參數(shù)和返回結(jié)果。但是它們在編寫和使用上有一些區(qū)別。下面將詳細介紹task和function的區(qū)別。 語法結(jié)構(gòu)
    的頭像 發(fā)表于 02-22 15:53 ?729次閱讀

    verilog中function和task的區(qū)別

    非常相似,但它們在功能和使用方式上有一些重要的區(qū)別。 定義和聲明方式不同: Function:使用關(guān)鍵字"function"來定義和聲明。函數(shù)可以有一個或多個輸入?yún)?shù),可以有一個返回值
    的頭像 發(fā)表于 02-22 15:40 ?1324次閱讀

    Samtec - 通過優(yōu)化焊膏模板開孔來擴大連接器的選擇范圍

    mm焊膏模板配合使用,同時在良品率為100%的情況下也能滿足IPC-J-STD-001 Class 2標準的要求。
    發(fā)表于 01-02 15:33 ?166次閱讀
    Samtec - 通過優(yōu)化焊膏<b class='flag-5'>模板</b>開孔來擴大連接器的選擇范圍

    verilog語法-如何使用function提高效率?

    function的作用返回一個數(shù)值,此數(shù)值由一串組合邏輯代碼計算得到。
    的頭像 發(fā)表于 12-25 15:27 ?1787次閱讀
    verilog語法-如何使用<b class='flag-5'>function</b>提高效率?

    如何使用CMW500測試頻譜模板

    在使用CMW500測試頻譜模板之前,首先我們需要了解什么是頻譜模板以及其在通信系統(tǒng)測試中的作用。頻譜模板是指在特定的頻率范圍內(nèi),記錄了該頻率范圍內(nèi)的信號功率譜密度的一種圖形表示。在通信系統(tǒng)測試中
    的頭像 發(fā)表于 12-25 15:10 ?1121次閱讀

    OpenCV邊緣模板匹配算法原理詳解

    OpenCV中自帶的模板匹配算法,完全是像素基本的模板匹配,特別容易受到光照影響,光照稍微有所不同,該方法就會歇菜了!搞得很多OpenCV初學者剛學習到該方法時候很開心,一用該方法馬上很傷心
    的頭像 發(fā)表于 12-07 10:56 ?1104次閱讀
    OpenCV邊緣<b class='flag-5'>模板</b>匹配算法原理詳解

    java的主必須是public

    一下Java程序的基本結(jié)構(gòu)。一個Java程序可以包含多個,但是只能有一個公共的作為主。這個主必須包含一個特殊的方法,即main方法,它是程序的入口點。當程序被執(zhí)行時,首先執(zhí)行m
    的頭像 發(fā)表于 11-28 16:15 ?4505次閱讀

    java抽象可以有普通方法嗎

    時使用abstract關(guān)鍵字,我們可以將該方法聲明為抽象方法。抽象方法沒有具體的實現(xiàn),只是定義了方法的簽名。子類繼承抽象后必須實現(xiàn)所有的抽象方法,否則子類也需要被聲明為抽象。抽象方
    的頭像 發(fā)表于 11-21 10:22 ?1235次閱讀

    std::randomize隨機結(jié)果不符合預(yù)期?

    在近期的一個testcase調(diào)試中,遇到一個std::randomize隨機結(jié)果不符合預(yù)期的現(xiàn)象。
    的頭像 發(fā)表于 10-29 15:57 ?1561次閱讀
    <b class='flag-5'>std</b>::randomize隨機結(jié)果不符合預(yù)期?

    SMT模板(鋼網(wǎng))的概述及特點

    模板的采購不僅是裝配工藝的步,它也是重要的一步。模板的主要功能是幫助錫膏的沉積(deposition)。目的是將準確數(shù)量的材料轉(zhuǎn)移到光板(bare PCB)上準確的位置。錫膏阻塞在模板上越少,沉積在電路板上就越多。
    發(fā)表于 10-12 15:12 ?653次閱讀

    如何通過模板模式重構(gòu)代碼

    法的某些特定步驟。 說人話:父模板方法定義不變的流程,子類重寫流程中的方法。 2、模板模式定義 ①、AbstractClass 抽象
    的頭像 發(fā)表于 10-08 16:15 ?364次閱讀
    如何通過<b class='flag-5'>模板</b>模式重構(gòu)代碼