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

            用模板的偏特化和成員模板,重載函數(shù)調(diào)用運(yùn)算符成功的實(shí)現(xiàn)了delegate,既可以綁定普通函數(shù),也可以綁定對(duì)象及其成員函數(shù)
            在cygnuwin下編譯通過, 
            還不支持一個(gè)delegate包含多個(gè)函數(shù)的用法,不過相信很簡(jiǎn)單,從std::list派生一個(gè)類 
            就可以了 
            我用的cygun有些毛病, 
            my_delegate d2=my_delegate(t,&Test::f); 
                                       ^如果寫成&t,就會(huì)導(dǎo)致編譯器內(nèi)部錯(cuò)誤,沒辦法了 
              
            我本來寫程序是加空行的,貼到BBS上就沒了,忍受一下吧 
            Win32下的各種調(diào)用約定很討厭,沒有考慮,不過實(shí)現(xiàn)起來不費(fèi)什么腦筋,就是麻煩, 
            不管了 
              
            // Test.cpp : Defines the entry point for the console application. 
            // 
            #include <stddef.h> 
            template<class T> 

            //函數(shù)traits,用來提取函數(shù)的返回類型

            struct function_traits 

            }; 
            template<class RT> 
            struct function_traits< RT(*)() > 

             typedef RT result_type; 
            }; 
            template<class RT,class AT> 
            struct function_traits< RT(*)(AT) > 

             typedef RT result_type; 
             typedef AT argument_type; 
            }; 
            template<class RT,class AT1,class AT2> 
            struct function_traits< RT(*)(AT1,AT2) > 

             typedef RT result_type; 
             typedef AT1 first_argument_type; 
             typedef AT2 second_argument_type; 
            };

            // 函數(shù)traits,用來提取類成員函數(shù)的返回類型


            template<class RT, class OT> 
            struct function_traits< RT (OT::*)() > 

             typedef OT object_type; 
             typedef RT result_type; 
            }; 
            template<class RT, class OT, class AT> 
            struct function_traits< RT (OT::*)(AT) > 

             typedef OT object_type; 
             typedef RT result_type; 
             typedef AT argument_type; 
              
             typedef AT first_argument_type; 
            }; 
            template<class RT,class OT,class AT1,class AT2> 
            struct function_traits< RT (OT::*)(AT1,AT2) > 

             typedef OT object_type; 
             typedef RT result_type; 
             typedef AT1 first_argument_type; 
             typedef AT2 second_argument_type; 
            };

            // 把一個(gè)普通函數(shù)類向轉(zhuǎn)化為類型兼容的指定類的成員函數(shù)類型 
            template <typename OT, typename PFT> 
            struct to_member_function_pointer 

            }; 
            template <typename OT,typename RT> 
            struct to_member_function_pointer< OT, RT(*)() > 

             typedef RT (OT::*type)(); 
            }; 
            template <typename OT, typename RT, typename AT> 
            struct to_member_function_pointer< OT, RT(*)(AT) > 

             typedef RT (OT::*type)(AT); 
            }; 
            template <typename OT, typename RT, typename AT1, typename AT2> 
            struct to_member_function_pointer< OT, RT(*)(AT1,AT2) > 

             typedef RT (OT::*type)(AT1,AT2); 
            }; 
            template <typename OT, typename RT, typename AT1, typename AT2, typename AT3> 
            struct to_member_function_pointer< OT, RT(*)(AT1,AT2,AT3) > 

             typedef RT (OT::*type)(AT1,AT2,AT3); 
            }; 

            // 轉(zhuǎn)化為const 成員函數(shù)

            template <typename OT, typename PFT> 
            struct to_const_member_function_pointer 

            }; 
            template <typename OT, typename RT> 
            struct to_const_member_function_pointer< OT, RT(*)() > 

             typedef RT (OT::*type)() const; 
            }; 
            template <typename OT, typename RT, typename AT> 
            struct to_const_member_function_pointer< OT, RT(*)(AT) > 

             typedef RT (OT::*type)(AT) const; 
            }; 
            template <typename OT, typename RT, typename AT1, typename AT2> 
            struct to_const_member_function_pointer< OT, RT(*)(AT1,AT2) > 

             typedef RT (OT::*type)(AT1,AT2) const; 
            }; 
            template <typename OT, typename RT, typename AT1, typename AT2, typename AT3> 
            struct to_const_member_function_pointer< OT, RT(*)(AT1,AT2,AT3) > 

             typedef RT (OT::*type)(AT1,AT2,AT3) const; 
            }; 

            // delegate的實(shí)現(xiàn)

            template <typename PFT> 
            class delegate 

             class object 
             { 
             }*m_pObject; // 對(duì)象指針,是一個(gè)代理對(duì)象
             typedef typename to_member_function_pointer<object, PFT>::type object_member_fuunction_pointer; 
             union 
             { 
              PFT m_pf; 
              object_member_function_pointer m_pmf; 
             }; // 函數(shù)指針和成員函數(shù)指針的聯(lián)合體
             public: 
              typedef typename function_traits<PFT>::result_type result_type; 
              
              delegate() 
              { 
               m_pObject=NULL; 
               m_pf=NULL; 
              } 
              
              delegate(PFT pf) 
              { 
               operator=(pf); 
              } 
              
              template<typename OT> 
               delegate( 
               OT *pObject, 
               typename to_member_function_pointer<OT, PFT>::type pmf 
               ) 
              { 
               m_pObject=reinterpret_cast<object*>(pObject); 
               m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf)); 
              } 
              
              template<typename OT> 
               delegate( 
               OT &pObject, 
               typename to_member_function_pointer<OT, PFT>::type pmf 
               ) 
              { 
               m_pObject=reinterpret_cast<object*>(&pObject); 
               m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf)); 
              } 
              
              template<typename OT> 
               delegate( 
               const OT *pObject, 
               typename to_const_member_function_pointer<OT, PFT>::type pmf 
               ) 
              { 
               m_pObject=const_cast<object*>(reinterpret_cast<object*>(pObject)); 
               m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf)); 
              } 
              
              template<typename OT> 
               delegate( 
               const OT &pObject, 
               typename to_const_member_function_pointer<OT, PFT>::type pmf 
               ) 
              { 
               m_pObject=const_cast<object*>(reinterpret_cast<object*>(&pObject)); 
               m_pmf=*(reinterpret_cast<object_member_function_pointer*>(&pmf)); 
              } 
              
              delegate & operator=(PFT pf) 
              { 
               m_pf=pf; 
               m_pObject=0; 
               return *this; 
              } 
              template<int> 
            :gcc的函數(shù)模板不許無參數(shù),加了個(gè)占位的"int"才能通過 
               result_type operator()() 
              { 
               if(m_pObject) 
                return (m_pObject->*m_pmf)(); 
               else 
                return m_pf(); 
              } 
              template<typename AT> 
               result_type operator()( 
               AT a1 
               ) 
              { 
               if(m_pObject) 
                return (m_pObject->*m_pmf)(a1); 
               else 
                return m_pf(a1); 
              } 
              template<typename AT1, typename AT2> 
               result_type operator()( 
               AT1 a1, 
               AT2 a2 
               ) 


               if(m_pObject) 
                return (m_pObject->*m_pmf)(a1,a2); 
               else 
                return m_pf(a1,a2); 
              } 
              template<typename AT1, typename AT2, typename AT3> 
               result_type operator()( 
               AT1 a1, 
               AT2 a2, 
               AT3 a3 
               ) 
              { 
               if(m_pObject) 
                return (m_pObject->*m_pmf)(a1,a2,a3); 
               else 
                return m_pf(a1,a2,a3); 
              } 
            }; 
            int gf(int) 

             return 0; 

            class Test 

            public: 
             int f(int){return 0;} 
            }; 
            typedef delegate < int (*)(int) > my_delegate; 
            int main() 

             Test t;

             

             my_delegate d1=&gf; // 普通函數(shù)
             my_delegate d2=my_delegate(t,&Test::f); //對(duì)象和類成員函數(shù)
             d1(0); //調(diào)用
             d2(2); 
            }

            posts - 94, comments - 138, trackbacks - 0, articles - 94

            Copyright © RichardHe

            欧美性猛交xxxx免费看久久久| 一个色综合久久| 精品国产乱码久久久久软件| 久久夜色精品国产噜噜亚洲a| 亚洲欧洲久久av| 9999国产精品欧美久久久久久| 久久久久亚洲AV成人网人人网站| 2021国内久久精品| 99久久婷婷国产综合亚洲| 久久久WWW成人| 久久久久亚洲av无码专区喷水| 久久精品成人一区二区三区| 伊人色综合久久天天网| 久久精品水蜜桃av综合天堂| 久久久久九九精品影院| 久久精品午夜一区二区福利 | 久久精品国产亚洲AV影院| 色诱久久久久综合网ywww| 久久久久无码国产精品不卡| 97久久国产综合精品女不卡| 99久久夜色精品国产网站| 色综合久久88色综合天天| 久久午夜羞羞影院免费观看| 久久国产乱子伦精品免费午夜| 久久精品麻豆日日躁夜夜躁| 偷窥少妇久久久久久久久| 国产精品成人无码久久久久久| 丰满少妇人妻久久久久久4| 久久亚洲AV无码精品色午夜麻豆 | 精品无码人妻久久久久久| 色综合久久综合网观看| 一本一道久久综合狠狠老| 久久久精品日本一区二区三区| 久久中文字幕一区二区| 国产美女亚洲精品久久久综合 | 伊人久久五月天| 亚洲精品乱码久久久久久不卡| 国内精品久久久久久中文字幕| 久久青青草原亚洲av无码app| 久久久久久免费视频| 欧美成人免费观看久久|