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

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            C++模版全掌握(實例)

            前段時間重新學習C++,主要看C++編程思想和C++設計新思維。對模版的使用有了更進一層的了解,特總結如下:

            下面列出了模版的常用情況:
            //1. 模板類靜態成員
            template <typename T> struct testClass
            {
               
            static int _data;
            }
            ;
            template
            <> int testClass<char>::_data = 1;
            template
            <> int testClass<long>::_data = 2;
            int main( void ) {
                cout
            << boolalpha << (1==testClass<char>::_data) << endl;
                cout
            << boolalpha << (2==testClass<long>::_data) << endl;
            }
             

            //2. 模板類偏特化
            template <class I, class O> struct testClass
            {
                testClass()
            { cout << "I, O" << endl; } 
            }
            ;
            template
            <class T> struct testClass<T*, T*> 
            {
                testClass()
            { cout << "T*, T*" << endl; } 
            }
            ;
            template
            <class T> struct testClass<const T*, T*> 
            {
                testClass()
            { cout << "const T*, T*" << endl; } 
            }
            ;
            int main( void )
            {
                testClass
            <int, char> obj1;
                testClass
            <int*, int*> obj2;
                testClass
            <const int*, int*> obj3;
            }
             

            //3.類模版+函數模版
            template <class T> struct testClass
            {
               
            void swap( testClass<T>& ) { cout << "swap()" << endl; } 
            }
            ;
            template
            <class T> inline void swap( testClass<T>& x, testClass<T>& y )
            {
                x.swap( y );
            }
             
            int main( void )
            {
                testClass
            <int> obj1;
                testClass
            <int> obj2;
                swap( obj1, obj2 );
            }
             


            //4. 類成員函數模板
            struct testClass
            {
                template
            <class T> void mfun( const T& t )
               
            {
                    cout
            << t << endl;
                }
             
                template
            <class T> operator T()
               
            {
                   
            return T();
                }
             
            }
            ;
            int main( void )
            {
                testClass obj;
                obj.mfun(
            1 );
               
            int i = obj;
                cout
            << i << endl;
            }
             

            //5. 缺省模板參數推導
            template <class T> struct test
            {
                T a;
            }
            ;
            template
            <class I, class O=test<I> > struct testClass
            {
                I b;
                O c;
            }
            ;

            void main()
            {
            }



            //6. 非類型模板參數
            template <class T, int n> struct testClass {
                T _t;
                testClass() : _t(n)
            {
                }
             
            }
            ;
            int main( void ) {
                testClass
            <int,1> obj1;
                testClass
            <int,2> obj2;
            }
             


            //7. 空模板參數
            template <class T> struct testClass;
            template
            <class T> bool operator==( const testClass<T>&, const testClass<T>& )
            {
               
            return false;
            }
            ;
            template
            <class T> struct testClass
            {
                friend
            bool operator== <>( const testClass&, const testClass& );
            }
            ;
            void main()
            {
            }


            //8. template template 類
            struct Widget1
            {
            template
            <typename T> 
                T foo()
            {} 
            }
            ;

            template
            <template<class T>class X> 
            struct Widget2
            {
            }
            ;
            void main()
            {
                cout
            << 3 << '\n';
            }




            //參考:http://www.cnblogs.com/dayouluo/archive/2005/05/14/155092.html

            特別注意:類,全局函數,類的成員函數都可以特化,但是只有類可以半特化,全局函數和類的成員函數不可以半特化。
            //-------------------------------------------
            //1 類的特化和類成員函數的特化
            template<typename T>
            class Widget1
            {
            public:
               
            void Fun1()
               
            {
                   
            //generic implementation
                }

               
            }
            ;

            template
            <>
            class Widget1<int>
            {
            public:
               
            void Fun1()
               
            {
                }

            }
            ;
            template
            <> 
            void Widget1<char>::Fun1()
            {
               
            //specialization
            }


            void main()
            {

              Widget1
            <char> w;
              w.Fun1();
              Widget1
            <int> w2;
              w2.Fun1();
             
            }

            //-------------------------------------------
            //2 全局函數的特化和重載
            template<typename T1, typename T2>
            T1 Fun2(T2)
            {
            }


            //下面2個應該是屬于重載
            template<typename T2>
            char Fun2(T2)
            {
               
            char c;
               
            return c;
            }


            template
            <typename T1>
            T1 Fun2(
            char)
            {
            }


            //全局函數的特化
            template<>
            char Fun2<char,int>(int)
            {
               
            char c;
               
            return c;
            }

            int main()
            {
            }

            //-------------------------------------------
            //3 全局函數不能半特化,以下編譯失敗
            template <typename T1,typename T2> //原型1
            void Test(T1,T2)
            {
            }


            template
            <typename T1>
            void Test<T1,T1>(T1,T1)
            {
            }


            template
            <typename T1, typename T2> //原型2
            T1 Fun2(T2)
            {
            }

            //
            template<typename T2>
            int Fun2<int,T2>(T2)
            {
            }

            template
            <typename T1>
            T1 Fun2
            <T1,int>(int)
            {
            }

            template
            <typename T>
            T Fun2
            <T,T>(T)
            {
            }

            int main()
            {
            }



            ////-------------------------------------------
            ////4 類可以特化和半特化,但是特的成員函數像全局函數一樣,只能特化,不能半特化,

            template<typename T1, typename T2> struct Widget2
            {
             
            void Fun2()
             
            {
                 
            //generic implementation
              }

            }
            ;

            template
            <typename T2> 
            struct Widget2<char,T2>
            {
               
            void Fun2()
               
            {
                }

            }
            ;

            template
            <typename T2>
            struct widget2
            {
               
            void Fun2()
               
            {
                   
            // class partial specialization
                }

            }
            ;



            //the class member funtion can not be partial specialization
            //以下的成員函數半特化,編譯失敗
            template<typename T2>
            void Widget2<char, T2>::Fun2()
            {
               
            //class member function partial specialization
            }

            int main()
            {
            }

            參考:C++編程思想2

            posted on 2008-09-14 17:37 肥仔 閱讀(2098) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            久久婷婷国产综合精品| 色诱久久久久综合网ywww| 国产福利电影一区二区三区久久老子无码午夜伦不| 亚洲中文久久精品无码| 久久夜色精品国产噜噜亚洲AV| 97精品国产97久久久久久免费| 秋霞久久国产精品电影院| 久久精品无码专区免费东京热| 婷婷伊人久久大香线蕉AV| 国产精品一区二区久久不卡| 久久激情五月丁香伊人| 综合久久一区二区三区 | 性做久久久久久久久| 久久精品综合网| 久久综合久久综合久久| 超级碰碰碰碰97久久久久| 88久久精品无码一区二区毛片 | 亚洲国产另类久久久精品黑人| 色欲久久久天天天综合网| AA级片免费看视频久久| 日韩精品久久无码中文字幕| 美女久久久久久| 国产福利电影一区二区三区久久老子无码午夜伦不| 欧美一区二区久久精品| 精品久久久久国产免费| 久久精品国产亚洲av麻豆小说| 亚洲精品国产第一综合99久久 | 亚洲人AV永久一区二区三区久久| 久久人人爽人人爽人人片AV不| 久久久久无码国产精品不卡| 久久久亚洲欧洲日产国码aⅴ| 亚洲中文字幕伊人久久无码| 国产精品99久久不卡| 7国产欧美日韩综合天堂中文久久久久| 欧美亚洲国产精品久久高清| 国产精品久久久久久吹潮| 天天影视色香欲综合久久| 青青久久精品国产免费看| 四虎国产精品免费久久| 91麻豆国产精品91久久久| 东方aⅴ免费观看久久av|