• <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吧,有更好的方式  回復  更多評論   
             
            国产午夜福利精品久久| 国产精品久久久久天天影视| 精品久久久久久| 久久午夜伦鲁片免费无码| 伊人色综合久久天天网| 97精品伊人久久大香线蕉| 久久91精品国产91久| 久久大香萑太香蕉av| 精品久久久久成人码免费动漫| 久久久无码精品亚洲日韩软件| 成人a毛片久久免费播放| 久久国产精品无码网站| 久久久久久久综合综合狠狠| 久久久久国产一区二区| 久久青青草视频| 少妇久久久久久久久久| 东京热TOKYO综合久久精品| 婷婷综合久久狠狠色99h| 久久精品无码一区二区日韩AV| 亚洲一级Av无码毛片久久精品| 久久精品国产亚洲αv忘忧草 | 久久99精品久久久久子伦| 国产午夜福利精品久久2021| 久久综合狠狠色综合伊人| 亚洲乱码日产精品a级毛片久久| 久久只这里是精品66| 久久99精品久久久久久久久久| 久久久综合九色合综国产| 亚洲伊人久久成综合人影院 | 很黄很污的网站久久mimi色 | 久久精品国产99久久丝袜| 一级做a爰片久久毛片免费陪| 久久人人爽人人爽人人片av高请| 99久久免费国产精精品| 91精品国产91久久| 免费精品国产日韩热久久| 99久久er这里只有精品18| 久久久久久久久66精品片| 久久精品国产亚洲av水果派| 欧美激情精品久久久久久久九九九| 青青草原精品99久久精品66|