• <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

            亚洲?V乱码久久精品蜜桃 | 久久久久99精品成人片牛牛影视| 国产精品女同久久久久电影院| 久久精品国产99久久久| 久久精品免费一区二区三区| 国内精品久久久久国产盗摄| 久久久久久久综合综合狠狠| 久久精品国产亚洲AV香蕉| 久久国产精品-久久精品| 久久久精品久久久久特色影视| 人妻精品久久无码区| 久久久久久亚洲精品无码| 久久久久亚洲av无码专区喷水| 国产69精品久久久久99尤物| 亚洲综合精品香蕉久久网| 99久久精品免费观看国产| 亚洲中文字幕久久精品无码喷水| 精品国产乱码久久久久久浪潮 | 久久亚洲欧美国产精品| 国产三级精品久久| 浪潮AV色综合久久天堂| 亚洲精品成人网久久久久久| 久久国产高清字幕中文| 久久天天躁狠狠躁夜夜网站| 一级a性色生活片久久无| 久久久久国产日韩精品网站| 2021国产成人精品久久| 国内精品久久国产大陆| 少妇高潮惨叫久久久久久| 三级韩国一区久久二区综合| AAA级久久久精品无码区| 久久se精品一区精品二区| AAA级久久久精品无码片| 色欲综合久久中文字幕网| 久久精品视频一| 无码专区久久综合久中文字幕| 久久伊人五月丁香狠狠色| 久久人做人爽一区二区三区| 亚洲精品成人久久久| 狠狠色丁香久久婷婷综合| 无码精品久久久久久人妻中字|