日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C 11很吊的新特性!std::function

 菌心說 2021-09-20

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 _Rpclass ..._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    functionoperator=(const function&);    functionoperator=(function&&) _NOEXCEPT;    functionoperator=(nullptr_t) _NOEXCEPT;    template<class _Fp, class = _EnableIfCallable<_Fp>>    functionoperator=(_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_infotarget_type() const _NOEXCEPT;    template <typename _Tp> _Tp* target() _NOEXCEPT;    template <typename _Tp> const _Tp* target() const _NOEXCEPT;#endif  // _LIBCPP_NO_RTTI

從成員函數(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ù)指針

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對(duì)象包裝函數(shù)指針    std::cout << callback(10) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的實(shí)體    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對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體    return 0;}

3、std::function包裝模板函數(shù)

template<typename 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對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體    return 0;}

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á)式

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

6、std::function包裝模板對(duì)象函數(shù)

template <typename T>struct sub{    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ù)

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

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ù)

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::placeholders::_1); //std::function包裝類成員函數(shù)    std::cout << callback(9) << std::endl; //std::function對(duì)象實(shí)例調(diào)用包裝的調(diào)用實(shí)體    return 0;}

這里我們用到了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{    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)

int main(int argc, char *argv[]){    std::cout << 'Hello world' << std::endl;    std::function<int(int)> callback2 = callback; //拷貝賦值運(yùn)算符    std::cout << callback2(7) << std::endl;    std::function<int(int)>&& callback3 = std::move(callback); //移動(dòng)賦值運(yùn)算符    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;}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多