• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數據加載中……

            CGL開發手記之三--CGL中的"函數對象"

                    需要注意的是,本節題目中"函數對象"四字是加了雙引號的,因為C語言中沒有辦法做到類C++中函數對象的做法,CGL中采用的替代手段是使用函數指針.

                    首先需要短暫回顧STL中的函數對象以及函數對象適配器等概念.

                    STL中的最基本的函數對象分為三類:generator, unary function,binary function,其中的generator是無參數函數,unary function是單參數函數,binary function是雙參數函數.注意,以上只是提到的三者的傳入參數的數量,還沒有涉及到它們所要返回的參數類型.一般的,三者都是以template參數作為自己返回參數的類型,但是當unary function和binary function返回參數是bool型的時候,分別稱它們為:predicate和binary predicate,這是因為STL中很多范型參數的調用需要傳入一個函數對象進行元素的比較.如sort,find_if等函數.

                    函數對象適配器分為兩類:一類是綁定器(binder),另外一類是取反器(negator),綁定器用于綁定函數對象中的某個參數,取反器用于將函數對象的返回值進行翻轉,一般的,可以使用取反器的函數對象都是那些返回值為bool型的函數對象.

                    CGL中不能實現函數適配器,因為綁定器在綁定參數的時候是通過將所需要綁定的參數作為模版參數傳遞給函數對象類進行參數綁定的,如:
            template <class Operation> 
            class binder2nd
              : 
            public unary_function<typename Operation::first_argument_type,
                                      
            typename Operation::result_type> {
            protected:
              Operation op;
              
            typename Operation::second_argument_type value;
            public:
              binder2nd(
            const Operation& x,
                        
            const typename Operation::second_argument_type& y) 
                  : op(x), value(y) {}
              
            typename Operation::result_type
              operator()(
            const typename Operation::first_argument_type& x) const {
                return op(x, value); 
              }
            };

            template 
            <class Operation, class T>
            inline binder2nd
            <Operation> bind2nd(const Operation& op, const T& x) {
              typedef 
            typename Operation::second_argument_type arg2_type;
              return binder2nd
            <Operation>(op, arg2_type(x));
            }

            以上是綁定器bind2nd函數的實現,你可以看到這個函數是返回一個binder2nd的函數對象類,而所需要綁定的參數作為binder2nd的value參數存儲起來,在調用binder2nd的operator()時再使用value參數,由于C中沒有辦法做到重載operator()函數,所以不能實現binder.

            同樣的,C中也沒有辦法實現negator函數對象適配器,見代碼:
            template <class Predicate>
            class unary_negate
              : 
            public unary_function<typename Predicate::argument_type, bool> {
            protected:
              Predicate pred;
            public:
              
            explicit unary_negate(const Predicate& x) : pred(x) {}
              bool operator()(
            const typename Predicate::argument_type& x) const {
                return !pred(x);
              }
            };

            template 
            <class Predicate>
            inline unary_negate
            <Predicate> not1(const Predicate& pred) {
              return unary_negate
            <Predicate>(pred);
            }

            以上是negator函數not1函數的實現.可以看到這個函數返回一個unary_negate函數對象,而在這個函數對象重載的operator()中對pred函數的返回值進行取反以達到negator函數對象適配器的作用,同樣的,用于C中不能做到重載operator(),所以沒有辦法實現negator函數對象適配器.

            STL中還有一些預定義的函數對象如plus,equal_to等,見代碼:
            template <class T>
            struct plus : 
            public binary_function<T, T, T> {
                T operator()(
            const T& x, const T& y) const { return x + y; }
            };

            plus的函數參數是通過模版參數T傳入的,任何想要采用plus的類型都需要提供operator + 操作符.因為這個特點,C中也沒有辦法實現這些預定義的函數對象.

                    鑒于以上的原因,CGL中的"函數對象"被定義為幾種函數指針:
                   
            typedef bool_t    (*binary_predicate)(data_t tData1, data_t tData2);
            typedef void    (
            *binary_func)(data_t tData1, data_t tData2, data_t tResult);
            typedef void    (
            *binary_func2)(data_t tData1, data_t tData2);

            typedef bool_t    (
            *unary_predicate)(data_t tData);
            typedef void    (
            *unary_func)(data_t tData, data_t tResult);
            typedef void    (
            *unary_func2)(data_t tData);

            typedef void    (
            *generator_func)(data_t tResult);
            typedef void    (
            *generator_func2)();

                 這些函數指針的返回值只有兩種,bool_t和void,當需要返回除了boot_t之外的返回值時,將保存返回值的指針tResult傳入函數中保存返回值,所有CGL中使用的函數指針都只可能是以上的幾種函數指針類型.
                    


            posted on 2007-05-07 16:53 那誰 閱讀(916) 評論(0)  編輯 收藏 引用 所屬分類: CGL

            色综合久久中文字幕综合网| 国产成人久久精品一区二区三区 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久免费精品一区二区| 久久久久久九九99精品| 久久精品国产半推半就| 久久99热这里只有精品国产| 久久久精品久久久久久| 精品国产青草久久久久福利| 精品久久人妻av中文字幕| 久久精品嫩草影院| 亚洲精品视频久久久| 久久人人爽爽爽人久久久| 免费国产99久久久香蕉| 中文字幕精品无码久久久久久3D日动漫 | 国产激情久久久久影院| 久久久高清免费视频| 久久精品国产久精国产| 日韩人妻无码一区二区三区久久99| 天堂久久天堂AV色综合| 久久精品亚洲乱码伦伦中文| 日本久久久久亚洲中字幕| 久久久久亚洲av成人无码电影| 亚洲国产一成人久久精品| 国产精品免费看久久久香蕉 | 欧洲成人午夜精品无码区久久| 欧美亚洲另类久久综合| 日韩精品久久久肉伦网站 | 久久久久久久久久免免费精品| 亚洲中文久久精品无码| 久久综合色老色| 久久久亚洲精品蜜桃臀| 国产成人精品久久亚洲| 国产成人久久精品一区二区三区| 久久婷婷五月综合色奶水99啪| 精品久久一区二区| 91久久香蕉国产熟女线看| 久久久久一区二区三区| 国产福利电影一区二区三区,免费久久久久久久精 | 香蕉久久永久视频| 污污内射久久一区二区欧美日韩 |