• <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>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            接上文
            下面看看TypeList的類型刪除功能
            相關源碼為:

            ////////////////////////////////////////////////////////////////////////////////
            // class template Erase
            // Erases the first occurence, if any, of a type in a typelist
            // Invocation (TList is a typelist and T is a type):
            // Erase<TList, T>::Result
            // returns a typelist that is TList without the first occurence of T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct Erase;
                    
                    template 
            <class T>                         // Specialization 1
                    struct Erase<NullType, T>
                    
            {
                        typedef NullType Result;
                    }
            ;

                    template 
            <class T, class Tail>             // Specialization 2
                    struct Erase<Typelist<T, Tail>, T>
                    
            {
                        typedef Tail Result;
                    }
            ;

                    template 
            <class Head, class Tail, class T> // Specialization 3
                    struct Erase<Typelist<Head, Tail>, T>
                    
            {
                        typedef Typelist
            <Head, 
                                typename Erase
            <Tail, T>::Result>
                            Result;
                    }
            ;

            正如所說
            Erase刪除的類型是第一次出現的類型
            其特化版本有3個第一個是針對空類型鏈表的刪除
            其二和其三的相互迭代構成了對所有位置類型的刪除動作

            TypeList的下一個操作是對鏈表的給定類型清空動作
            ////////////////////////////////////////////////////////////////////////////////
            // class template EraseAll
            // Erases all first occurences, if any, of a type in a typelist
            // Invocation (TList is a typelist and T is a type):
            // EraseAll<TList, T>::Result
            // returns a typelist that is TList without any occurence of T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct EraseAll;
                    template 
            <class T>
                    
            struct EraseAll<NullType, T>
                    {
                        typedef NullType Result;
                    };
                    template 
            <class T, class Tail>
                    
            struct EraseAll<Typelist<T, Tail>, T>
                    {
                        
            // Go all the way down the list removing the type
                        typedef typename EraseAll<Tail, T>::Result Result;
                    };
                    template 
            <class Head, class Tail, class T>
                    
            struct EraseAll<Typelist<Head, Tail>, T>
                    {
                        
            // Go all the way down the list removing the type
                        typedef Typelist<Head, 
                                typename EraseAll
            <Tail, T>::Result>
                            Result;
                    };

            接著,下面的代碼實現的功能分別是
            1.刪除類型鏈表中所有重復的類型
            2.類型替換的解決

            1.刪除類型鏈表中重復的類型
            代碼為:
            ////////////////////////////////////////////////////////////////////////////////
            // class template NoDuplicates
            // Removes all duplicate types in a typelist
            // Invocation (TList is a typelist):
            // NoDuplicates<TList, T>::Result
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct NoDuplicates;
                    
                    template 
            <> struct NoDuplicates<NullType>
                    {
                        typedef NullType Result;
                    };

                    template 
            <class Head, class Tail>
                    
            struct NoDuplicates< Typelist<Head, Tail> >
                    {
                    
            private:
                        typedef typename NoDuplicates
            <Tail>::Result L1;
                        typedef typename Erase
            <L1, Head>::Result L2;
                    
            public:
                        typedef Typelist
            <Head, L2> Result;
                    };
            如果鏈表為空則不操作
            如果鏈表為非空則進行以下動作
            刪除位置位置為1,2,3,..的重復類型

            下面的功能則是類型替換和鏈表翻轉
            ////////////////////////////////////////////////////////////////////////////////
            // class template Replace
            // Replaces the first occurence of a type in a typelist, with another type
            // Invocation (TList is a typelist, T, U are types):
            // Replace<TList, T, U>::Result
            // returns a typelist in which the first occurence of T is replaced with U
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T, class U> struct Replace;
                    
                    template 
            <class T, class U>
                    
            struct Replace<NullType, T, U>
                    {
                        typedef NullType Result;
                    };

                    template 
            <class T, class Tail, class U>
                    
            struct Replace<Typelist<T, Tail>, T, U>
                    {
                        typedef Typelist
            <U, Tail> Result;
                    };

                    template 
            <class Head, class Tail, class T, class U>
                    
            struct Replace<Typelist<Head, Tail>, T, U>
                    {
                        typedef Typelist
            <Head,
                                typename Replace
            <Tail, T, U>::Result>
                            Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template ReplaceAll
            // Replaces all occurences of a type in a typelist, with another type
            // Invocation (TList is a typelist, T, U are types):
            // Replace<TList, T, U>::Result
            // returns a typelist in which all occurences of T is replaced with U
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T, class U> struct ReplaceAll;
                    
                    template 
            <class T, class U>
                    
            struct ReplaceAll<NullType, T, U>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class T, class Tail, class U>
                    
            struct ReplaceAll<Typelist<T, Tail>, T, U>
                    {
                        typedef Typelist
            <U, typename ReplaceAll<Tail, T, U>::Result> Result;
                    };
                    
                    template 
            <class Head, class Tail, class T, class U>
                    
            struct ReplaceAll<Typelist<Head, Tail>, T, U>
                    {
                        typedef Typelist
            <Head,
                                typename ReplaceAll
            <Tail, T, U>::Result>
                            Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template Reverse
            // Reverses a typelist
            // Invocation (TList is a typelist):
            // Reverse<TList>::Result
            // returns a typelist that is TList reversed
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct Reverse;
                    
                    template 
            <>
                    
            struct Reverse<NullType>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class Head, class Tail>
                    
            struct Reverse< Typelist<Head, Tail> >
                    {
                        typedef typename Append
            <
                            typename Reverse
            <Tail>::Result, Head>::Result Result;
                    };

            類型鏈表的最后一個設計的功能是鑒別類型鏈表中有多少個類型繼承于給定類型
            ////////////////////////////////////////////////////////////////////////////////
            // class template MostDerived
            // Finds the type in a typelist that is the most derived from a given type
            // Invocation (TList is a typelist, T is a type):
            // MostDerived<TList, T>::Result
            // returns the type in TList that's the most derived from T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct MostDerived;
                    
                    template 
            <class T>
                    
            struct MostDerived<NullType, T>
                    {
                        typedef T Result;
                    };
                    
                    template 
            <class Head, class Tail, class T>
                    
            struct MostDerived<Typelist<Head, Tail>, T>
                    {
                    
            private:
                        typedef typename MostDerived
            <Tail, T>::Result Candidate;
                    
            public:
                        typedef typename Select
            <
                            SuperSubclass
            <Candidate,Head>::value,
                                Head, Candidate
            >::Result Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template DerivedToFront
            // Arranges the types in a typelist so that the most derived types appear first
            // Invocation (TList is a typelist):
            // DerivedToFront<TList>::Result
            // returns the reordered TList 
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct DerivedToFront;
                    
                    template 
            <>
                    
            struct DerivedToFront<NullType>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class Head, class Tail>
                    
            struct DerivedToFront< Typelist<Head, Tail> >
                    {
                    
            private:
                        typedef typename MostDerived
            <Tail, Head>::Result
                            TheMostDerived;
                        typedef typename Replace
            <Tail,
                            TheMostDerived, Head
            >::Result Temp;
                        typedef typename DerivedToFront
            <Temp>::Result L;
                    
            public:
                        typedef Typelist
            <TheMostDerived, L> Result;
                    };
            DerivedToFront是排列類型鏈表保持后面的類型繼承于前面的類型
            全文測試代碼如下:
            #include <iostream>
            #include 
            <string>
            #include 
            <Loki/TypeList.h>
            #include 
            <typeinfo>

            typedef 
            int Type;
            typedef Loki::TL::MakeTypelist
            <Type,
                                           
            char,
                                           
            long,
                                           
            bool,
                                           
            int,
                                           std::
            string,
                                           
            double,
                                           unsigned 
            int,
                                           
            long long,
                                           
            int> MyList;
            class Class{};
            class Class1: public Class{};
            class Class2: public Class{};                               
            class Class3: public Class{};

            int main()
            {
                MyList::Result hlist;
                std::cout 
            <<"MyList length "<<Loki::TL::Length<MyList::Result>::value<<std::endl; 
                Loki::TL::TypeAt
            <MyList::Result,1>::Result result;
                std::cout
            <<"the type in indexo of 1: "<<result<<std::endl; 
                Loki::TL::TypeAtNonStrict
            <MyList::Result,0>::Result _type; 
                std::cout
            <<"default value in index of 0:" <<_type<<std::endl;
                std::cout
            <<Loki::TL::IndexOf<MyList::Result,long>::value<<std::endl; 
                typedef Loki::TL::Append
            <MyList::Result,Class>::Result NewType;
                std::cout 
            <<"get length of NewType: "<< Loki::TL::Length<NewType>::value<<std::endl; 
                typedef Loki::TL::Erase
            <MyList::Result,double>::Result NewType2;
                std::cout
            <<"new length of NewType:"<<Loki::TL::Length<NewType2>::value<<std::endl; 
                typedef Loki::TL::EraseAll
            <MyList::Result,int>::Result NewType3;
                std::cout
            <<"new length of NewType after erase all int type:"<<Loki::TL::Length<NewType3>::value<<std::endl; 
                typedef Loki::TL::NoDuplicates
            <MyList::Result>::Result NewType4;
                std::cout
            <<"new length of New type after NoDuplicates "<<Loki::TL::Length<NewType4>::value<<std::endl;
                typedef Loki::TL::Replace
            <MyList::Result,int,Class>::Result NewType5;
                typedef Loki::TL::Append
            <MyList::Result,Class1>::Result NewType6;
                typedef Loki::TL::Append
            <MyList::Result,Class2>::Result NewType7;
                typedef Loki::TL::Append
            <MyList::Result,Class3>::Result NewType8;
                typedef Loki::TL::Append
            <MyList::Result,Class2>::Result NewType9;
                typedef Loki::TL::Append
            <MyList::Result,long>::Result NewType10;
                
                Loki::TL::MostDerived
            <NewType10,int>::Result r;
                std::cout
            <<typeid(Loki::TL::MostDerived<NewType10,Class>::Result).name()<<std::endl;
                Loki::TL::DerivedToFront
            <NewType10>::Result t; 
                std::cout
            <<"new length of New type after Arrange TypeList "<<Loki::TL::Length<NewType10>::value<<std::endl;
                
            int len = Loki::TL::Length<NewType10>::value;
                std::cout
            <<"the type name in given lindex : "<<typeid(Loki::TL::TypeAtNonStrict<MyList::Result,11>::Result).name()<<std::endl;
                system(
            "PAUSE");
                
            return EXIT_SUCCESS;
            }

            Loki TypeList寫完了
            感覺到了模板元編程的強大之處
            posted on 2010-04-17 10:23 ccsdu2009 閱讀(2000) 評論(4)  編輯 收藏 引用
            Comments
            • # re: Loki技法3-Typelist(2)[未登錄]
              tiny
              Posted @ 2010-04-17 19:29
              去看boost的模板元吧。比loki舒服  回復  更多評論   
            • # re: Loki技法3-Typelist(2)
              ccsdu2009
              Posted @ 2010-04-17 19:51
              @tiny
              boost是個龐然大物
              loki比較小

                回復  更多評論   
            • # re: Loki技法3-Typelist(2)
              99書城
              Posted @ 2010-04-18 14:59
              是對方空間是打開解放  回復  更多評論   
            • # re: Loki技法4-Typelist(2)[未登錄]
              無名
              Posted @ 2010-04-22 19:31
              去看看C++0x吧,有更好的方式  回復  更多評論   
             
            色综合久久中文字幕无码| 精品久久久久久无码中文野结衣| 国内精品久久久久影院网站| 精品久久人人爽天天玩人人妻| 久久久久亚洲精品中文字幕| 精品无码久久久久久久久久| 激情久久久久久久久久| 久久www免费人成看国产片| 国产精品99久久精品爆乳| 国产精品免费久久久久久久久 | 奇米影视7777久久精品| 日产精品久久久久久久性色| 久久99精品综合国产首页| 大蕉久久伊人中文字幕| 欧美久久亚洲精品| 亚洲va久久久噜噜噜久久狠狠| 国产V亚洲V天堂无码久久久| 国产免费久久精品丫丫| 亚洲第一永久AV网站久久精品男人的天堂AV | 女同久久| 久久久久久亚洲Av无码精品专口| 久久精品国产亚洲av高清漫画| 色综合久久综合网观看| 欧美亚洲另类久久综合婷婷| 亚洲精品美女久久久久99| 欧美久久精品一级c片片| 色偷偷88欧美精品久久久| 色妞色综合久久夜夜| 久久久久99精品成人片三人毛片| 色妞色综合久久夜夜| 久久亚洲精品视频| 久久精品久久久久观看99水蜜桃| 久久99国产精品久久99果冻传媒| 久久天天躁狠狠躁夜夜不卡| 97热久久免费频精品99| 伊人 久久 精品| 国产免费久久精品99久久| 国产成人久久AV免费| 亚洲午夜久久久久久久久电影网| 精品久久久久久无码人妻热| 99久久国产综合精品麻豆|