• <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++Templates The Complete Guide"讀書筆記----Chapter 7

            Basic Template Terminology
            1. "Class Template" or "Template Class"?
            class template: the class is a template.
            template class: a synonym(同義詞) for class template; to refer to classes generated from templates(由模板產(chǎn)生(實例化)的類). to refer to classes with a name that is a template-id.
            Because of this imprecision, template class is avoided in this book.

            2. Instantiation and specialzation
            Instantiation: 實例化. 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.
            Specialzation:特例,在Chapter 3中有描述

            3. Declarations vs Definitions
            declaration is a C++ construct that introduces or reintroduceds a name into? a C++ scope.
            class?C;?//?a?declaration?of?C?as?a?class
            void?f(int?p);?//?a?declaration?of?f()?as?a?function?and?p?as?a?named?parameter
            extern?int?v;?//?a?declaration?of?v?as?a?variable

            Declarations become definitions when the details of their structure are made known or, in the case of variables, when storage space must be allocated.
            class?C{};?//?definition(and?declaration)?of?class?C
            void?f(int?p){?//?defini
            ???std::cout<<?p?<<?std::endl;
            }


            extern?int?v?=?1;??//?an?initializer?makes?this?a?definition?for?v

            int?w;???//?global?variable?declaration?not?preceded?by?extern?are?also?definitions
            By extension, the declaration of?a class template or function template is called a definition if it has a body, Hence,
            template?<typename?T>
            void?func(T);
            is a declaration that is not a definition, whereas
            template?<typename?T>
            class?S{};
            is in fact a definition

            posted @ 2006-12-03 15:02 ningfangli 閱讀(111) | 評論 (0)編輯 收藏

            "C++Templates The Complete Guide"讀書筆記----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 @ 2006-12-03 12:38 ningfangli 閱讀(176) | 評論 (0)編輯 收藏

            網(wǎng)上推薦的殺毒組合

            殺毒就用卡巴:(不敢恭維,原來用過,很耗資源)
            木馬就用Ewido:(剛裝上,破解比較麻煩,文件中有整個包,看到大家的評價不錯,官方下載:http://download.ewido.net/ewido-setup.exe;)
            防火墻就用ZoneAlarm:? (還是不用的好,這個好麻煩,總要你確認是否允許聯(lián)網(wǎng),就用xp的防火墻吧)

            這三個合起來用,可以使得系統(tǒng)堅若磐石
            它們每個都是當今世界最強的,不用是你的損失!!

            卡巴和ZoneAlarm同時安裝時,記得在卡巴的實時保護設(shè)置里,把那個禁止網(wǎng)絡(luò)攻擊保護打上鉤!!!!!!不然會沖突!!! 切記!!!(如果只安裝卡巴的話就不用這樣做了)

            posted @ 2006-12-02 01:30 ningfangli 閱讀(181) | 評論 (0)編輯 收藏

            不勝其煩的網(wǎng)絡(luò)病毒!

            今天晚上,已經(jīng)是我兩周以來第二次恢復(fù)筆記本的系統(tǒng)了,好在有true image啊!。原因都是一樣:在上網(wǎng)找一些軟件時,不幸中招。
            我還一直很小心不斷升級Symantec。將防火墻開啟。也真不知道哪個防毒軟件真的好使,也不知道怎么這么多人希望弄這些病毒!
            無奈!

            posted @ 2006-12-02 00:46 ningfangli 閱讀(104) | 評論 (0)編輯 收藏

            "C++Templates The Complete Guide"讀書筆記----Chapter 5

                 摘要: 1. To access a type name that depends on a template parameter, you have to?qualify(?修改,修飾) the name with a leading typename//?print?elements?of?an?STL?containertemplate?<typename?T>void?printcol...  閱讀全文

            posted @ 2006-12-01 15:41 ningfangli 閱讀(166) | 評論 (0)編輯 收藏

            "C++Templates The Complete Guide"讀書筆記----Chapter 4

            Chapter 4 Nontype Template Parameters
            1. Templates can have template parameters that are values rather than types
            With this class, user of the stack could specify this size of the array as the maximum size needed for stack elements
            template?<typename?T,?int?MAXSIZE>
            class?Stack?{
            ??
            private:
            ????T?elems[MAXSIZE];????????
            //?elements
            ????int?numElems;????????????//?current?number?of?elements

            ??
            public:
            ????Stack();??????????????????
            //?constructor
            ????void?push(T?const&);??????//?push?element
            ????void?pop();???????????????//?pop?element
            ????T?top()?const;????????????//?return?top?element
            ????bool?empty()?const?{??????//?return?whether?the?stack?is?empty
            ????????return?numElems?==?0;
            ????}

            ????
            bool?full()?const?{???????//?return?whether?the?stack?is?full
            ????????return?numElems?==?MAXSIZE;
            ????}

            }
            ;

            //?constructor
            template?<typename?T,?int?MAXSIZE>
            Stack
            <T,MAXSIZE>::Stack?()
            ??:?numElems(
            0)???????????????//?start?with?no?elements
            {
            ????
            //?nothing?else?to?do
            }


            template?
            <typename?T,?int?MAXSIZE>
            void?Stack<T,MAXSIZE>::push?(T?const&?elem)
            {
            ????
            if?(numElems?==?MAXSIZE)?{
            ????????
            throw?std::out_of_range("Stack<>::push():?stack?is?full");
            ????}

            ????elems[numElems]?
            =?elem;???//?append?element
            ????++numElems;???????????????//?increment?number?of?elements
            }


            template
            <typename?T,?int?MAXSIZE>
            void?Stack<T,MAXSIZE>::pop?()
            {
            ????
            if?(numElems?<=?0)?{
            ????????
            throw?std::out_of_range("Stack<>::pop():?empty?stack");
            ????}

            ????
            --numElems;???????????????//?decrement?number?of?elements
            }


            template?
            <typename?T,?int?MAXSIZE>
            T?Stack
            <T,MAXSIZE>::top?()?const
            {
            ????
            if?(numElems?<=?0)?{
            ????????
            throw?std::out_of_range("Stack<>::top():?empty?stack");
            ????}

            ????
            return?elems[numElems-1];??//?return?last?element
            }
            2. You can also define nontype parameters for function templates.
            template?<typename?T,?int?VAL>
            T?addValue?(T?
            const&?x)
            {
            ????
            return?x?+?VAL;
            }
            However, according to the current standard, sets of overloaded functions cannot? be used for template parameter deduction. Thus, you have to cast to the exact type of the function template argument:
            std::transform(source.begin(),source.end(),//?start?and?end?of?source
            ???????????????dest.begin(),//?start?of?destination
            ???????????????(int(*)(int?const&))addValue<int,5>);//operation
            There is a proposal for the standard to fix this behavior so that the cast isn't necessary.
            2. You cannot use floating-point numbers, class-type objects, and objects with internal linkage(such as string literals) as arguments for nontype template parameters

            posted @ 2006-11-26 19:50 ningfangli 閱讀(137) | 評論 (0)編輯 收藏

            斷點失效解決方法

            http://www.controlav.com/bphelper/start.asp

            posted @ 2006-11-26 17:02 ningfangli 閱讀(126) | 評論 (0)編輯 收藏

            "C++Templates The Complete Guide"讀書筆記----Chapter 3

            Chapter 3 Class Templates
            1. A clas template is a class that is implemented with one or more type parameters left open
            #include?<vector>
            #include?
            <stdexcept>

            template?
            <typename?T>
            class?Stack?{
            ??
            private:
            ????std::vector
            <T>?elems;?????//?elements

            ??
            public:
            ????
            void?push(T?const&);??????//?push?element
            ????void?pop();???????????????//?pop?element
            ????T?top()?const;????????????//?return?top?element
            ????bool?empty()?const?{??????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            template?
            <typename?T>
            void?Stack<T>::push?(T?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            template
            <typename?T>
            void?Stack<T>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            template?
            <typename?T>
            T?Stack
            <T>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range("Stack<>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }
            2. To use a class template, you pass the open types as template arguments. The class template is the instantiated(and compiled) for these types
            3. For class templates, only those menber funcitons that are called are instantiated
            This , of course, saves time and space. It has the additional benefit that you can instantiate a class even for those types that cannot perform all the operations of all the menber functions, as long as these member functions are not called.
            Note that you have to put whitespace between the two closing? template brackets. If you don't do this, you are using operator>>,which results in a syntax error:
            Stack<Stack<int>>?intStackStack;?//ERROR:>>is?not?allowed
            4. You can specialize calss templates for certain types
            template<>
            class?Stack<std::string>?{
            ??
            private:
            ????std::deque
            <std::string>?elems;??//?elements

            ??
            public:
            ????
            void?push(std::string?const&);??//?push?element
            ????void?pop();?????????????????????//?pop?element
            ????std::string?top()?const;????????//?return?top?element
            ????bool?empty()?const?{????????????//?return?whether?the?stack?is?empty
            ????????return?elems.empty();
            ????}

            }
            ;

            void?Stack<std::string>::push?(std::string?const&?elem)
            {
            ????elems.push_back(elem);????
            //?append?copy?of?passed?elem
            }


            void?Stack<std::string>::pop?()
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::pop():?empty?stack");
            ????}

            ????elems.pop_back();?????????
            //?remove?last?element
            }


            std::
            string?Stack<std::string>::top?()?const
            {
            ????
            if?(elems.empty())?{
            ????????
            throw?std::out_of_range
            ????????????????(
            "Stack<std::string>::top():?empty?stack");
            ????}

            ????
            return?elems.back();??????//?return?copy?of?last?element
            }


            ps:也就是所謂的特定版本,模板元編程中的關(guān)鍵。
            5. You can partially specialize class templates for certain types
            If more than one partial specilalization matches equally well, the declaration is ambiguous
            6. You can define default values for class template parameters.These may refer to previous template parameters

            posted @ 2006-11-26 16:15 ningfangli 閱讀(165) | 評論 (0)編輯 收藏

            "C++Templates The Complete Guide"讀書筆記----Chapter 2

            這段時間在通過"C++Templates The Complete Guide"這本書學(xué)習(xí)Templates。發(fā)現(xiàn)這本書確實不錯:語言簡明,內(nèi)容翔實。特別是每章后面的Summery總結(jié)得很好。這份讀書筆記就已這個Summery為基礎(chǔ)寫的。

            書前面的Prefece和Chapter 1就跳過了。既然來學(xué)習(xí)Templates就已經(jīng)知道它的重要性了。

            ??????????????????????????????? Chapter 2 FunctionTemplates
            1. Function templates define a family of functions for different template arguments;

            template? < typename?T >
            inline?T?
            const & ?max?(T? const & ?a,?T? const & ?b)
            {
            ????
            // ?if?a?<?b?then?use?b?else?use?a
            ???? return ??a? < ?b? ? ?b?:?a;
            }

            2. When you pass template arguments, function templates are instantiated for these argument types.
            The process of replacing templates parameters by concrete types is called instantiatin.(at compiled time)
            3. You can explicitly qualify the template parameters

            {
            // ?because?no?automatic?type?conversion?if?allowed?in?templates,so
            max(static_cast < double > ( 4 ), 4.2 ) // cast?the?arguments?so?that?they?both?match
            max < double > ( 4 , 4.2 ) // ?specify?explicitly?the?type?of?T
            }

            4. You can overload funciton templates

            inline? int ? const & ?max?( int ? const & ?a,? int ? const & ?b)?
            {
            ????
            return ??a? < ?b? ? ?b?:?a;
            }


            // ?maximum?of?two?values?of?any?type
            template? < typename?T >
            inline?T?
            const & ?max?(T? const & ?a,?T? const & ?b)
            {
            ????
            return ??a? < ?b? ? ?b?:?a;
            }


            // ?maximum?of?three?values?of?any?type
            template? < typename?T >
            inline?T?
            const & ?max?(T? const & ?a,?T? const & ?b,?T? const & ?c)
            {
            ????
            return ?::max?(::max(a,b),?c);
            }


            int ?main()
            {
            ????::max(
            7 ,? 42 ,? 68 );????? // ?calls?the?template?for?three?arguments
            ????::max( 7.0 ,? 42.0 );????? // ?calls?max<double>?(by?argument?deduction)
            ????::max( ' a ' ,? ' b ' );?????? // ?calls?max<char>?(by?argument?deduction)
            ????::max( 7 ,? 42 );????????? // ?calls?the?nontemplate?for?two?ints
            ????::max <> ( 7 ,? 42 );??????? // ?calls?max<int>?(by?argument?deduction)
            ????::max < double > ( 7 ,? 42 );? // ?calls?max<double>?(no?argument?deduction)
            ????::max( ' a ' ,? 42.7 );????? // ?calls?the?nontemplate?for?two?ints
            }

            ps: the overload resolution process normally prefers this nontemplate over one generated from the template. the fourth call falls under this rule.
            5. When you overload function templates, limit your changes to specifying template parameters explicitly

            {
            max
            <> ( 7 , 42 ); // call?max<int>?(by?argument?deduction)
            }


            6. Make sure you see all overloaded versions of funciton templates before you call them

            template? < typename?T >
            inline?T?
            const & ?max?(T? const & ?a,?T? const & ?b)
            {
            ????
            return ??a? < ?b? ? ?b?:?a;
            }


            // ?maximum?of?three?values?of?any?type
            template? < typename?T >
            inline?T?
            const & ?max?(T? const & ?a,?T? const & ?b,?T? const & ?c)
            {
            ????
            return ?max?(max(a,b),?c);?? // ?uses?the?template?version?even?for?ints
            }
            ?????????????????????????????? // ?because?the?following?declaration?comes
            ???????????????????????????????
            // ?too?late:
            // ?maximum?of?two?int?values
            inline? int ? const & ?max?( int ? const & ?a,? int ? const & ?b)?
            {
            ????
            return ??a? < ?b? ? ?b?:?a;
            }


            ?

            posted @ 2006-11-26 15:24 ningfangli 閱讀(180) | 評論 (0)編輯 收藏

            建立這個站點

            ? 昨天晚上建立了這個博客。主要想把關(guān)于自己學(xué)習(xí)C++的知識、人生的感悟、還有從網(wǎng)上看到的好東西等等都放在這里。但建立的時候為博客的名字,我和老婆絞盡腦汁。想的名字不是太俗,就是太怪。于是我們就想到問問我兩歲半的兒子,正處于睡意懵懂中的兒子大聲說:“寧帥!”。哈哈。這個是兒子給自己起的名字。好就叫這個了。至于“健康、快樂、勇敢”是我們對他的希望。

            posted @ 2006-11-23 10:51 ningfangli 閱讀(116) | 評論 (0)編輯 收藏

            僅列出標題
            共4頁: 1 2 3 4 

            導(dǎo)航

            統(tǒng)計

            公告

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

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久亚洲国产精品一区二区| 天天综合久久一二三区| 亚洲欧美一区二区三区久久| 久久精品国产亚洲一区二区三区| 久久99国产精品一区二区| 狠狠88综合久久久久综合网| www性久久久com| 99国产欧美精品久久久蜜芽| 国产精品久久久久久一区二区三区| 精品久久久久久无码专区不卡| 久久亚洲欧美国产精品| 九九精品99久久久香蕉| 日本道色综合久久影院| 亚洲精品高清国产一久久| 精品熟女少妇aⅴ免费久久| 久久久久亚洲精品无码网址| 久久天天日天天操综合伊人av| 亚洲精品97久久中文字幕无码| 狠狠色丁香久久婷婷综合_中 | 精产国品久久一二三产区区别| 久久伊人五月天论坛| 无码人妻精品一区二区三区久久久| 亚洲精品高清国产一线久久| 久久国产精品99久久久久久老狼| 国产精品99久久久久久www| 思思久久99热免费精品6| 久久久无码精品亚洲日韩按摩| 青青青青久久精品国产| 四虎亚洲国产成人久久精品| 99精品国产综合久久久久五月天| 国产综合久久久久| 亚洲精品成人久久久| 2021精品国产综合久久| 久久五月精品中文字幕| 77777亚洲午夜久久多喷| 欧美激情精品久久久久久| 久久99国内精品自在现线| 午夜福利91久久福利| 国产精品久久久久久福利漫画| 久久人人爽人人爽人人片AV高清| 一级做a爱片久久毛片|