• <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>
            隨筆 - 42  文章 - 3  trackbacks - 0
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(2)

            隨筆檔案

            文章檔案

            網(wǎng)頁(yè)收藏

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            This article is for reading of C++ Templates The Complete Guide.
             
            1 Define a function template:
            template <typename T>
            inline T const& max (T const& a, T const& b)
            {
               return a<b?b:a;
            }
            As seen in this example, template parameters must be announced with syntax of the following form:
            template < comma-separated-list-of-parameters >, the keyword typename introduces a so-called type parameter.

            2 Calling to a function template:
            int i = 42;
            std::cout << "max(7,i): " << ::max(7,i) << std::endl;
            double f1 = 3.4;
            double f2 = -6.7;
            std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;
            std::string s1 = "mathematics";
            std::string s2 = "math";
            std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
            Normally, templates aren't compiled into single entities that can handle any type. Instead, different entities are generated from the template for every type for which the template is used.


            3 Note that no automatic type conversion is allowed here. Each must match exactly. For example:
            template <typename T>
            inline T const& max (T const& a, T const& b);

            max(4,7) // OK: T is int for both arguments
            max(4,4.2) // ERROR: first T is int, second T is double
            There are three ways to handle such an error:
            Cast the arguments so that they both match: max(static_cast<double>(4),4.2) // OK
            Specify (or qualify) explicitly the type of T: max<double>(4,4.2) // OK

            4 Class template usage:

            template <typenameTarget>
            class Singleton
            {
            public:
            static Target* Instance() //Static template member function should be impleted in the define of template
            {                                       //notes: it is not a thread safe
             if (m_pInstance==NULL)
              m_pInstance = new Target();

             return m_pInstance;
            }

            protected: //!!set the constructor and destructor to be protected to avoid unnecessary intance
             Singleton(); // constructor
             ~Singleton(); // destructor
            private:
             static  Target* m_pInstance;
            };

            //static class member initialisation should be global
            template <typename Target>
            Target* Singleton<Target>::m_pInstance = NULL;

            int main(int argc, _TCHAR* argv[])
            {
             int* testSingle = Singleton<int>::Instance();
             std::cout<<*testSingle<<std::endl;
            }

            4.1 Nontype Class Template Parameters
            Notype parameter provides a special way to define the property of template. But you cannot use floating-point numbers, class-type objects, and objects with internal linkage (such as string
            literals) as arguments for nontype template parameterstemplate <typename T, int MAXSIZE>
            class Stack {
            private:
            T elems[MAXSIZE]; // elements
            int numElems; // current number of elements
            }
            Stack<int,20> int20Stack; // stack of up to 20 ints

            6.1 The way that including declare and implemention of template in the header file is inclusion model. This kind of model is popular in the development. 
            7.1 The process of creating a regular class, function, or member function from a template by substituting actual values for its arguments is called template instantiation. This resulting entity (class, function, or member function) is generically called a specialization.
            posted on 2012-06-08 18:42 鷹擊長(zhǎng)空 閱讀(208) 評(píng)論(0)  編輯 收藏 引用

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


            久久亚洲精精品中文字幕| 亚洲AV日韩精品久久久久久久| 99久久精品九九亚洲精品| 久久99热精品| 手机看片久久高清国产日韩 | 久久久久AV综合网成人| 欧美午夜A∨大片久久| 久久人妻少妇嫩草AV蜜桃| 777米奇久久最新地址| 青青青青久久精品国产h久久精品五福影院1421| 狠狠色婷婷久久综合频道日韩| 亚洲AV日韩精品久久久久久| 精品久久久久一区二区三区| 一本一道久久综合狠狠老| 精品多毛少妇人妻AV免费久久| 国产亚洲精久久久久久无码77777| 久久精品国产亚洲欧美| 色综合久久久久无码专区| 久久精品国产亚洲Aⅴ香蕉| 久久精品水蜜桃av综合天堂| 天堂无码久久综合东京热| 7国产欧美日韩综合天堂中文久久久久| 欧美伊人久久大香线蕉综合| 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品国产亚洲av麻豆小说| 欧美日韩精品久久久久| 九九久久精品无码专区| 久久免费线看线看| 久久不射电影网| 狠狠色婷婷久久一区二区三区| 亚洲国产精品无码久久久不卡| 天天影视色香欲综合久久| 久久久久97国产精华液好用吗| 99久久精品国产一区二区蜜芽| 香蕉久久一区二区不卡无毒影院| 国产欧美久久久精品| 久久99国产精一区二区三区| 久久99精品国产99久久| 国产精品综合久久第一页| 久久精品国产99久久久香蕉| 欧美激情精品久久久久久|