• <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
            <2010年3月>
            28123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            留言簿(2)

            隨筆檔案

            文章檔案

            網頁收藏

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            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 鷹擊長空 閱讀(209) 評論(0)  編輯 收藏 引用
            无码人妻久久一区二区三区免费丨 | 国产成年无码久久久免费| 亚洲国产精品狼友中文久久久| 亚洲伊人久久成综合人影院 | 亚洲人成电影网站久久| 久久久久亚洲av成人网人人软件 | 欧美久久精品一级c片片| 欧美性猛交xxxx免费看久久久| 精品伊人久久久| 色综合久久中文综合网| 欧洲成人午夜精品无码区久久| 久久国产精品-久久精品| 影音先锋女人AV鲁色资源网久久 | 2021国内久久精品| 久久国产精品一区二区| 亚洲欧洲日产国码无码久久99| 国产精品成人99久久久久| 久久亚洲国产成人精品性色| 久久亚洲精品国产亚洲老地址| 99国产欧美久久久精品蜜芽| 一本一道久久综合狠狠老| 午夜精品久久久久久影视riav| 国产成人综合久久久久久 | 久久婷婷五月综合色99啪ak| 国产精品久久久久久福利漫画| 久久亚洲精品国产精品婷婷| 久久精品不卡| 久久性生大片免费观看性| 99久久成人18免费网站| 国产精品99久久久久久人| 亚洲精品乱码久久久久久久久久久久 | 精品午夜久久福利大片| 97久久综合精品久久久综合| 久久婷婷五月综合97色| 日产精品久久久久久久| 色婷婷综合久久久中文字幕| 国产精品乱码久久久久久软件| 久久亚洲国产最新网站| 国内精品久久久久久久久电影网 | 亚洲国产成人久久综合碰| 欧美亚洲日本久久精品|