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

            健康,快樂(lè),勇敢的寧帥!!

            努力、努力、再努力! 沒(méi)有什么能阻止我對(duì)知識(shí)的渴望。

             

            "C++Templates The Complete Guide"讀書(shū)筆記----Chapter 6

            Using Templates in Practice

            1. Templates challenge the classic compiler-plus-linker model. Therefore there are different approaches to organize template code: the inclusion model, explicit instantiation, and the separation model
            Most C and C++ programmers organize their nontemplate code largely as follows:
            1) Classes and other types are entirely placed in header files.
            2) For global variables and (noninline) functions, only a declaration is put in a header file, and the definition goes into a so-called dot-C file.
            The works well: It makes the needed type definition easily available throughout the program and avoids duplicate definition errors on variables and functions from the linker.
            With the convention in mind, we declare the template in a header file:
            #ifndef?MYFIRST_HPP
            #define?MYFIRST_HPP

            //?declaration?of?template
            template?<typename?T>?
            void?print_typeof?(T?const&);

            #endif?//?MYFIRST_HPP
            The implementation of the function is placed in a dot-C file:
            #include?<iostream>
            #include?
            <typeinfo>
            #include?
            "myfirst.hpp"

            //?implementation/definition?of?template
            template?<typename?T>
            void?print_typeof?(T?const&?x)
            {
            ????std::cout?
            <<?typeid(x).name()?<<?std::endl;
            }
            Finally, we use the template in another dot-C file, into which our template declaration is #include:
            #include?"myfirst.hpp"

            //?use?of?the?template
            int?main()
            {
            ????
            double?ice?=?3.0;
            ????print_typeof(ice);??
            //?call?function?template?for?type?double
            }

            A C++ compiler will most likely accept this program without any problems,but the linker will probably report an error, implying that there is no definition of the function print_typeof().
            In order for a template to be? instantiated, the compiler must know which definition should be instantiated and for what template arguments it should be instantiated. Unfortunately, these two pieces of information are in files that are compiled separatedly. Therefore, when our compiler sees the call to print_typeof() but has no definition in sight to instantiate this function for double, it just assumes that such a definition is provided elsewhere and creates a reference(for the linker to resolve,linker) to that definition. On the other hand, when the compiler processes the file myfirst.cpp, it has no indication at that point that it must instantiate the template definition it contains for specific arguments.

            The Inclusion Model
            Rewrite the header-file, including template definition.
            #ifndef?MYFIRST_HPP
            #define?MYFIRST_HPP


            #include?
            <iostream>
            #include?
            <typeinfo>

            //?declaration?of?template
            template?<typename?T>?
            void?print_typeof?(T?const&
            );

            //?implementation/definition?of?template

            template?<typename?T>
            void?print_typeof?(T?const&?x)
            {
            ????std::cout?
            <<?typeid(x).name()?<<
            ?std::endl;
            }


            #endif?//?MYFIRST_HPP
            disadvantage: the cost is not the result of size of the template definition itself, but the result of the fact that we must also include the header used by the definition of our template-in the case <iostream> and <typeinfo>.\

            Explicit Instantiation
            To avoid above linker error we can add the following file to our program:
            #include?"myfirst.cpp"

            //?explicitly?instantiate?print_typeof()?for?type?double
            template?void?print_typeof<double>(double?const&);
            disadvantage: We must carefully keep track of which entities to instantiate. For large projects this quickly becomes an excessive burden.
            advantage:
            the instantiation can be tuned to the needs of the program.
            The overhead of large header is avoided.
            The source code of template definition can be kept hidden, but then no additional instantiations can be created by a client program.
            Finally, for some applications it can be useful to control the exact location(that is, the object file)of a template instance.

            Separation model

            2. Usually you should use the inclusion model
            3. By separating template code into different header files for declarations and definitions, you can more easily switch between the inclusion model and explicit instantiation
            4. The C++ standard defines a separate compilation model for templates(using the keyword export). It is not yet widely available, however.
            5. To take advantage of precompiled headers, be sure to keep the same order for #include directives.
            6. Debuggin code with templates can be challenging
            7. Template instances may have very long names

            posted on 2006-12-03 12:38 ningfangli 閱讀(177) 評(píng)論(0)  編輯 收藏 引用


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


            導(dǎo)航

            統(tǒng)計(jì)

            公告

            Dict.CN 在線詞典, 英語(yǔ)學(xué)習(xí), 在線翻譯

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            综合久久一区二区三区 | 一本色道久久综合狠狠躁篇| 欧美777精品久久久久网| 品成人欧美大片久久国产欧美| 久久99国产精品成人欧美| 日批日出水久久亚洲精品tv| 午夜天堂av天堂久久久| 一本伊大人香蕉久久网手机| 欧美久久久久久午夜精品| 亚洲综合伊人久久综合| 99久久亚洲综合精品成人| 青青草原综合久久大伊人导航| 久久99精品国产麻豆宅宅| 欧美精品一区二区精品久久| 少妇久久久久久被弄到高潮| 91精品国产色综合久久| 四虎久久影院| 国产精品免费看久久久香蕉| 一本色道久久综合狠狠躁| 草草久久久无码国产专区| 亚洲成色WWW久久网站| 亚洲国产精品综合久久网络| 国产精品久久久天天影视| 久久人人爽人人爽人人片AV不| 伊人久久大香线蕉影院95| 久久综合亚洲欧美成人| 久久精品成人欧美大片| 久久国产影院| 久久99精品国产麻豆婷婷| 久久亚洲欧美日本精品| 久久99精品久久只有精品| 午夜人妻久久久久久久久| 久久精品国产亚洲av麻豆蜜芽| 欧美久久综合九色综合| 久久精品中文字幕有码| 伊人色综合久久天天| 久久精品视频网| 亚洲国产精久久久久久久| 久久精品国产精品亚洲精品| 国产成人久久激情91| 亚洲精品高清久久|