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

            ////////////////////////////////////////////////////////////////////////////////
            // 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刪除的類型是第一次出現(xiàn)的類型
            其特化版本有3個(gè)第一個(gè)是針對(duì)空類型鏈表的刪除
            其二和其三的相互迭代構(gòu)成了對(duì)所有位置類型的刪除動(dòng)作

            TypeList的下一個(gè)操作是對(duì)鏈表的給定類型清空動(dòng)作
            ////////////////////////////////////////////////////////////////////////////////
            // 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;
                    };

            接著,下面的代碼實(shí)現(xiàn)的功能分別是
            1.刪除類型鏈表中所有重復(fù)的類型
            2.類型替換的解決

            1.刪除類型鏈表中重復(fù)的類型
            代碼為:
            ////////////////////////////////////////////////////////////////////////////////
            // 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;
                    };
            如果鏈表為空則不操作
            如果鏈表為非空則進(jìn)行以下動(dòng)作
            刪除位置位置為1,2,3,..的重復(fù)類型

            下面的功能則是類型替換和鏈表翻轉(zhuǎn)
            ////////////////////////////////////////////////////////////////////////////////
            // 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;
                    };

            類型鏈表的最后一個(gè)設(shè)計(jì)的功能是鑒別類型鏈表中有多少個(gè)類型繼承于給定類型
            ////////////////////////////////////////////////////////////////////////////////
            // 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是排列類型鏈表保持后面的類型繼承于前面的類型
            全文測(cè)試代碼如下:
            #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寫完了
            感覺到了模板元編程的強(qiáng)大之處
            posted on 2010-04-17 10:23 ccsdu2009 閱讀(2005) 評(píng)論(4)  編輯 收藏 引用
            Comments

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            久久精品国产亚洲沈樵| 狠狠色丁香久久婷婷综合五月| 久久99热狠狠色精品一区| 91精品国产91久久久久久青草| 亚洲人成无码久久电影网站| 性欧美大战久久久久久久久 | 久久久久国产一级毛片高清板| 久久无码人妻精品一区二区三区| 色偷偷88888欧美精品久久久| 久久九九青青国产精品| 狠狠色噜噜色狠狠狠综合久久| 91精品国产色综久久| 人妻精品久久无码专区精东影业| 国产精品成人久久久久三级午夜电影| 久久精品无码一区二区WWW| 久久这里只精品国产99热| 欧洲成人午夜精品无码区久久| 久久一区二区三区免费| 996久久国产精品线观看| 亚洲精品乱码久久久久久蜜桃图片| 国产精品VIDEOSSEX久久发布| 国内精品久久久久影院免费| 狠狠综合久久综合88亚洲| 精品国产乱码久久久久软件| 久久精品亚洲欧美日韩久久| 97超级碰碰碰碰久久久久| 久久国产精品99久久久久久老狼 | 99久久国产主播综合精品| a高清免费毛片久久| 丰满少妇人妻久久久久久| 少妇精品久久久一区二区三区| 国产成年无码久久久免费| 伊人色综合九久久天天蜜桃| 久久夜色精品国产亚洲av| 久久久久亚洲av成人无码电影| 久久精品视频网| 狠狠色综合网站久久久久久久| 狠狠人妻久久久久久综合蜜桃| 爱做久久久久久| 欧美亚洲国产精品久久久久| 蜜桃麻豆WWW久久囤产精品|