• <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>
            posts - 297,  comments - 15,  trackbacks - 0
            If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template.

            For example, let's suppose that we have a very simple class called mycontainer that can store one element of any type and that it has just one member function called increase, which increases its value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation with a function member uppercase, so we decide to declare a class template specialization for that type:

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            // template specialization
            #include <iostream>
            using namespace std;

            // class template:
            template <class T>
            class mycontainer {
            T element;
            public:
            mycontainer (T arg) {element=arg;}
            T increase () {return ++element;}
            };

            // class template specialization:
            template <>
            class mycontainer <char> {
            char element;
            public:
            mycontainer (char arg) {element=arg;}
            char uppercase ()
            {
            if ((element>='a')&&(element<='z'))
            element+='A'-'a';
            return element;
            }
            };

            int main () {
            mycontainer<int> myint (7);
            mycontainer<char> mychar ('j');
            cout << myint.increase() << endl;
            cout << mychar.uppercase() << endl;
            return 0;
            }
            8
            J


            This is the syntax used in the class template specialization:

             
            template <> class mycontainer <char> { ... };


            First of all, notice that we precede the class template name with an empty template<> parameter list. This is to explicitly declare it as a template specialization.

            But more important than this prefix, is the <char> specialization parameter after the class template name. This specialization parameter itself identifies the type for which we are going to declare a template class specialization ( char). Notice the differences between the generic class template and the specialization:

            1
            2
            template <class T> class mycontainer { ... };
            template <> class mycontainer <char> { ... };


            The first line is the generic template, and the second one is the specialization.

            When we declare specializations for a template class, we must also define all its members, even those exactly equal to the generic template class, because there is no "inheritance" of members from the generic template to the specialization.

            from:
            http://www.cplusplus.com/doc/tutorial/templates/

            posted on 2010-05-03 22:11 chatler 閱讀(451) 評論(0)  編輯 收藏 引用 所屬分類: Template
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關(guān),覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久噜噜噜久久中文字幕色伊伊| 7国产欧美日韩综合天堂中文久久久久| 久久99精品久久久久久不卡| 久久免费香蕉视频| 久久久久亚洲AV无码麻豆| 成人精品一区二区久久久| 伊人色综合久久天天人守人婷| 久久久久国产精品熟女影院| 国产精品激情综合久久| 久久精品免费一区二区| 国产真实乱对白精彩久久| 婷婷综合久久中文字幕蜜桃三电影 | 久久精品视频网| 少妇熟女久久综合网色欲| 久久精品蜜芽亚洲国产AV| 亚洲人成无码www久久久| 国产91色综合久久免费| 久久无码AV一区二区三区| 久久综合欧美成人| 久久精品国产亚洲av麻豆色欲| 久久噜噜久久久精品66| 国产精品9999久久久久| 亚洲精品无码成人片久久| 久久一本综合| 久久久WWW成人免费精品| 99精品国产在热久久无毒不卡| 欧美日韩精品久久免费| 91亚洲国产成人久久精品网址| 久久精品国产亚洲AV电影| 久久亚洲日韩看片无码| 一个色综合久久| 久久综合久久综合亚洲| 久久久久黑人强伦姧人妻| 国内精品久久久久久久coent| 久久精品国产免费一区| 国产精品美女久久久久久2018| 无码国内精品久久人妻| 少妇人妻88久久中文字幕| 亚洲精品美女久久久久99| 色诱久久久久综合网ywww| 日韩精品无码久久久久久|