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

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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

            前段時(shí)間重新學(xué)習(xí)C++,主要看C++編程思想和C++設(shè)計(jì)新思維。對(duì)模版的使用有了更進(jìn)一層的了解,特總結(jié)如下:

            下面列出了模版的常用情況:
            //1. 模板類(lèi)靜態(tài)成員
            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. 模板類(lèi)偏特化 
            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
            <intchar> obj1; 
                testClass
            <int*int*> obj2; 
                testClass
            <const int*int*> obj3; 
            }
             

            //3.類(lèi)模版+函數(shù)模版
            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. 類(lèi)成員函數(shù)模板 
            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. 缺省模板參數(shù)推導(dǎo) 
            template <class T> struct test 

                T a; 
            }

            template 
            <class I, class O=test<I> > struct testClass 

                I b; 
                O c; 
            }


            void main()
            {
            }



            //6. 非類(lèi)型模板參數(shù) 
            template <class T, int n> struct testClass 
                T _t; 
                testClass() : _t(n) 

                }
             
            }

            int main( void ) 
                testClass
            <int,1> obj1; 
                testClass
            <int,2> obj2; 
            }
             


            //7. 空模板參數(shù) 
            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 類(lèi)
            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

            特別注意:類(lèi),全局函數(shù),類(lèi)的成員函數(shù)都可以特化,但是只有類(lèi)可以半特化,全局函數(shù)和類(lèi)的成員函數(shù)不可以半特化。
            //-------------------------------------------
            //1 類(lèi)的特化和類(lèi)成員函數(shù)的特化
            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 全局函數(shù)的特化和重載
            template<typename T1, typename T2>
            T1 Fun2(T2)
            {
            }


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


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


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

            int main()
            {
            }

            //-------------------------------------------
            //3 全局函數(shù)不能半特化,以下編譯失敗
            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 類(lèi)可以特化和半特化,但是特的成員函數(shù)像全局函數(shù)一樣,只能特化,不能半特化,

            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
            //以下的成員函數(shù)半特化,編譯失敗
            template<typename T2>
            void Widget2<char, T2>::Fun2()
            {
                
            //class member function partial specialization
            }

            int main()
            {
            }

            參考:C++編程思想2

            posted on 2007-05-24 16:24 夢(mèng)在天涯 閱讀(4260) 評(píng)論(7)  編輯 收藏 引用 所屬分類(lèi): CPlusPlus

            評(píng)論

            # re: C++模版全掌握(實(shí)例) 2007-05-25 17:10 pass86

            HAO.  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例) 2007-06-10 17:46 黃大仙

            好  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例) 2007-06-10 19:07 黃大仙

            收藏  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例) 2007-06-17 15:29 yapon

            以下是否可行?這種用意的類(lèi)模板定義有沒(méi)有其他方法?

            template<typename T>
            class A
            {
            public:
            class TT : public T
            {
            }
            };  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例) 2007-06-25 16:12 as

            不可行 TT繼承T 必須是T已經(jīng)存在的前提下  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例) 2007-06-25 16:13 as

            不可行吧  回復(fù)  更多評(píng)論   

            # re: C++模版全掌握(實(shí)例)[未登錄](méi) 2009-05-19 13:30 Mad

            非常好的總結(jié)  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類(lèi)

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1811173
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            一本久道久久综合狠狠躁AV| 久久亚洲精品无码VA大香大香| 国产精品久久国产精麻豆99网站| 亚洲αv久久久噜噜噜噜噜| 国产精品久久久久AV福利动漫| 九九热久久免费视频| 久久午夜免费视频| 久久棈精品久久久久久噜噜| 久久精品国产第一区二区| 久久精品国产99国产精品亚洲| 国产亚洲欧美精品久久久| 久久亚洲AV无码西西人体| 国内精品久久久久影院一蜜桃| 久久久久国产| 91精品国产综合久久四虎久久无码一级| 久久这里只有精品视频99| 久久777国产线看观看精品| 久久久精品国产免大香伊| 久久久久久久综合日本| 日本三级久久网| 久久久青草久久久青草| 99久久99久久精品国产片果冻 | 久久91精品国产91| AV无码久久久久不卡蜜桃 | 久久精品中文字幕有码| 97精品国产97久久久久久免费 | 久久免费视频观看| 色欲av伊人久久大香线蕉影院| 久久久久久久综合综合狠狠| 青青草国产成人久久91网| 久久人爽人人爽人人片AV| 少妇精品久久久一区二区三区| 奇米影视7777久久精品人人爽| 久久亚洲高清综合| 亚洲国产成人久久精品99 | 伊人热热久久原色播放www| 色婷婷综合久久久久中文字幕 | 伊人久久大香线蕉av不卡| 亚洲婷婷国产精品电影人久久| 久久精品这里只有精99品| 午夜精品久久久久9999高清|