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

            健康,快樂,勇敢的寧帥!!

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

             

            "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?container
            template?<typename?T>
            void?printcoll?(T?const&?coll)
            {
            ????typename?T::const_iterator?pos;??
            //?iterator?to?iterate?over?coll
            ????typename?T::const_iterator?end(coll.end());??//?end?position

            ????
            for?(pos=coll.begin();?pos!=end;?++pos)?{
            ????????std::cout?
            <<?*pos?<<?'?';
            ????}

            ????std::cout?
            <<?std::endl;
            }

            2. Nested classes and member functions can also be templates. One application is the ability to implement generic operations with internal type conversions. However, type checking still occurs.
            class?Stack?{
            ??
            private:
            ????std::deque
            <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();
            ????}


            ????
            //?assign?stack?of?elements?of?type?T2
            ????template?<typename?T2>
            ????Stack
            <T>&?operator=?(Stack<T2>?const&);
            }
            ;

            template?
            <typename?T>
            ?template?
            <typename?T2>
            Stack
            <T>&?Stack<T>::operator=?(Stack<T2>?const&?op2)
            {
            ????
            if?((void*)this?==?(void*)&op2)?{????//?assignment?to?itself?
            ????????return?*this;
            ????}


            ????Stack
            <T2>?tmp(op2);??????????????//?create?a?copy?of?the?assigned?stack

            ????elems.clear();???????????????????
            //?remove?existing?elements
            ????while?(!tmp.empty())?{???????????//?copy?all?elements
            ????????elems.push_front(tmp.top());
            ????????tmp.pop();
            ????}

            ????
            return?*this;
            }


            3. Template versions of assignment operators don't replace default assignment operators

            4. You can also use class templates as template parameters, as so-called template template parameters
            To use a different internal container for stacks, the application programmer has to specify the element type twice. Thus, to specify the type of the internal container, you have to pass the type of the container and the type of its elements again:
            Stack<int,std::vector<int>?>?vStack;?//?integer?stack?that?uses?a?vector
            Using template template parameters allows you to declare the Stack class template by spcecifying the type of the container without respecifying the type of its elements:
            stack<int,?std::vector>?vStack;?//?integer?stack?the?uses?a?vector
            To do this you must specify the second template parameter as a template template parameter.
            template?<typename?T,
            ??????????template?
            <typename?ELEM>?class?CONT?=?std::deque?>
            class?Stack?{
            ??
            private:
            ????CONT
            <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();
            ????}

            }
            ;
            The different is that the second template parameter is declare as being a class template:
            template?<typename?ELEM>?class?CONT

            5. Template template arguments must match exactly. Default template arguments of template template arguments are ignored
            The problem in this example is that the std::deque template of the standard library has more than one parameter: the second parameter has a default value, but this is not considered when match std::deque to the CONT parameter.
            We can rewrite te class declaration so that the CONT parameter expects containers with two template parameters:
            template?<typename?T,
            ??????????template?
            <typename?ELEM,?
            ????????????????????typename?ALLOC
            =?std::allocator<ELEM>?>
            ????????????????????
            class?CONT?=?std::deque>
            class?Stack?{
            ??
            private:
            ????CONT
            <T>?elems;?????????//?elements

            }
            ;
            ?6. By explicitly calling a default constructor, you can make sure that variables and members of templates are initialized by a default value even if they are instantiated with a built-in type.
            7.? For string literals there is an array-to-pointer conversion during argument deduction if and only if the parameter is not a reference
            Passing string literal arguments for reference parameters of templates sometimes fails in a surprising way.
            //?note:?reference?parameters
            template?<typename?T>
            inline?T?
            const&?max?(T?const&?a,?T?const&?b)
            {
            ????
            return??a?<?b?????b?:?a;
            }


            int?main()
            {
            ????std::
            string?s;

            ????::max(
            "apple","peach");???//?OK:?same?type
            ????::max("apple","tomato");??//?ERROR:?different?types
            ????::max("apple",s);?????????//?ERROR:?different?types
            }
            The problem is that string literals have defferent array depending on their lengths.
            However, if you declare nonreference parameters, you can substitute them with string literals of different size:
            //?note:?nonreference?parameters
            template?<typename?T>
            inline?T?max?(T?a,?T?b)
            {
            ????
            return??a?<?b?????b?:?a;
            }


            int?main()
            {
            ????std::
            string?s;

            ????::max(
            "apple","peach");???//?OK:?same?type
            ????::max("apple","tomato");??//?OK:?decays?to?same?type
            ????::max("apple",s);?????????//?ERROR:?different?types
            }
            The explanation for this behavior is that during argument deduction array-to-pointer conversion(often called decay) occurs only if the parameter does not have a reference type.

            template?<typename?T>
            void?ref?(T?const&?x)
            {
            ????std::cout?
            <<?"x?in?ref(T?const&):?"??
            ??????????????
            <<?typeid(x).name()?<<?'\n';
            }


            template?
            <typename?T>
            void?nonref?(T?x)
            {
            ????std::cout?
            <<?"x?in?nonref(T):?????"
            ??????????????
            <<?typeid(x).name()?<<?'\n';
            }


            int?main()
            {
            ????
            ref("hello");
            ????nonref(
            "hello");
            }

            the output might be as follows:
            x in ref(T const&): char[6]

            x in nonref(T): const char *

            posted on 2006-12-01 15:41 ningfangli 閱讀(165) 評(píng)論(0)  編輯 收藏 引用


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


            導(dǎo)航

            統(tǒng)計(jì)

            公告

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

            常用鏈接

            留言簿(4)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品国产精品青草| 区久久AAA片69亚洲| 亚洲国产精品成人AV无码久久综合影院 | 久久综合九色综合97_久久久| 久久97久久97精品免视看秋霞| 亚洲精品无码久久千人斩| 久久高清一级毛片| www性久久久com| 国产毛片欧美毛片久久久| 久久激情亚洲精品无码?V| 久久久久久人妻无码| 一本一道久久a久久精品综合| 国产精品欧美久久久天天影视| 久久香综合精品久久伊人| 久久久久久久国产免费看| 97久久精品无码一区二区天美| 久久久久久久久久久久久久| 久久久久久久亚洲精品| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久婷婷国产综合精品| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | av无码久久久久久不卡网站| 97r久久精品国产99国产精| 亚洲国产成人久久综合一区77 | 久久综合综合久久97色| 久久久精品国产免大香伊| 午夜精品久久久久久影视777| 91精品国产高清久久久久久国产嫩草 | 久久国产精品免费一区| 久久青青草原综合伊人| 久久91综合国产91久久精品| 亚洲国产欧洲综合997久久| 久久人人爽人人爽人人片av高请 | 99久久精品免费看国产一区二区三区| 色诱久久久久综合网ywww | 久久男人中文字幕资源站| 亚洲国产精品久久66| 久久精品一区二区国产| 蜜桃麻豆www久久| 亚洲国产精品久久久久| 精品国产一区二区三区久久蜜臀|