青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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

努力、努力、再努力! 沒有什么能阻止我對知識的渴望。

 

"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 閱讀(167) 評論(0)  編輯 收藏 引用

導航

統計

公告

Dict.CN 在線詞典, 英語學習, 在線翻譯

常用鏈接

留言簿(4)

隨筆檔案

文章分類

文章檔案

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲一级二级| 欧美三区美女| 亚洲欧洲日产国码二区| 久久亚洲高清| 久久久精品性| 久久精品人人做人人爽| 欧美综合77777色婷婷| 亚洲乱码一区二区| 久久福利影视| 久久久av水蜜桃| 久久婷婷丁香| 免费国产一区二区| 欧美精品久久一区二区| 欧美精品一区二区高清在线观看| 另类综合日韩欧美亚洲| 久久精品视频播放| 久久伊人一区二区| 欧美国产视频在线| 国产精品久久久久久久久搜平片| 国产精品日韩在线| 国产欧美一区二区在线观看| 国内精品伊人久久久久av影院 | 亚洲视频二区| 亚洲视频免费在线| 亚洲欧美日韩在线综合| 久久手机免费观看| 日韩系列欧美系列| 久久国产精品黑丝| 欧美日韩精品二区| 激情小说另类小说亚洲欧美| 亚洲精品小视频在线观看| 亚洲一区三区在线观看| 欧美一站二站| 免费看av成人| 亚洲一区日韩在线| 久久只精品国产| 国产精品久久午夜夜伦鲁鲁| 亚洲黄一区二区| 性欧美1819sex性高清| 欧美国产视频在线观看| 亚洲女同同性videoxma| 欧美极品aⅴ影院| 黄色精品免费| 欧美一区二区三区播放老司机| 亚洲国产精品久久久久婷婷884| 亚洲欧美国产精品桃花| 欧美日本不卡高清| 亚洲人成网站精品片在线观看| 欧美在线播放| 99精品视频网| 欧美精品亚洲二区| 亚洲人成网在线播放| 久久综合久久综合久久| 91久久久在线| 久久香蕉国产线看观看网| 国产精品亚洲视频| 国产精品99久久久久久人| 91久久精品www人人做人人爽| 久久久久综合| 激情久久久久久久久久久久久久久久| 中文网丁香综合网| 亚洲精品免费在线| 久久av红桃一区二区小说| 亚洲韩国青草视频| 麻豆精品传媒视频| 久久激情中文| 一区二区三区在线视频免费观看| 欧美一区2区三区4区公司二百| 日韩一区二区福利| 欧美日韩一区二区三区高清| 999在线观看精品免费不卡网站| 欧美成人激情视频| 老司机凹凸av亚洲导航| 一区在线观看| 欧美激情在线有限公司| 久久在精品线影院精品国产| 亚洲电影有码| 亚洲日本欧美日韩高观看| 欧美激情一区| 亚洲欧美高清| 午夜精品一区二区三区在线播放| 国产九区一区在线| 免费影视亚洲| 欧美肥婆在线| 亚洲欧美另类综合偷拍| 亚洲欧美卡通另类91av| 国产视频久久久久| 免费不卡在线观看av| 欧美阿v一级看视频| 一本色道久久综合亚洲精品按摩 | 一区二区不卡在线视频 午夜欧美不卡'| 欧美日韩色婷婷| 香港成人在线视频| 久久久青草青青国产亚洲免观| 91久久精品一区二区三区| 99精品久久免费看蜜臀剧情介绍| 国产精品久久久久久久久久三级| 久久久久久亚洲精品不卡4k岛国| 久久婷婷综合激情| 亚洲综合丁香| 浪潮色综合久久天堂| 亚洲专区一区| 久久久久中文| 久久成人亚洲| 欧美日韩免费网站| 奶水喷射视频一区| 国产精品大全| 欧美激情四色| 国产亚洲欧洲| 99国产精品国产精品久久| 狠狠久久综合婷婷不卡| 99亚洲一区二区| 在线观看日韩av| 亚洲一区视频在线| 99re6热只有精品免费观看| 欧美一级网站| 亚洲免费在线视频| 欧美精品一区二区三区视频| 久久九九精品99国产精品| 欧美日韩一区二区三区在线观看免| 一区二区成人精品 | 亚洲国产日韩欧美在线图片| 欧美寡妇偷汉性猛交| 在线亚洲高清视频| 亚洲国产精品成人一区二区| 亚洲综合视频一区| 在线亚洲高清视频| 欧美成人免费网| 蜜臀99久久精品久久久久久软件| 国产精品捆绑调教| 亚洲久久在线| 亚洲精品在线看| 男男成人高潮片免费网站| 老司机aⅴ在线精品导航| 国产欧美日韩三级| 亚洲一区二区三区免费视频| 日韩一级大片| 欧美人成在线视频| 亚洲老板91色精品久久| 91久久精品日日躁夜夜躁国产| 欧美在线免费视屏| 久久大逼视频| 国产一区二区三区av电影| 亚洲女人天堂av| 久久精品噜噜噜成人av农村| 国产精品夜夜夜| 亚洲一区黄色| 久久久久91| 亚洲国产导航| 欧美精品www在线观看| 亚洲精品在线三区| 夜夜嗨av一区二区三区免费区| 欧美日本乱大交xxxxx| 亚洲裸体视频| 性伦欧美刺激片在线观看| 国产亚洲日本欧美韩国| 久久精品最新地址| 亚洲成在线观看| 亚洲风情在线资源站| 黄网动漫久久久| 久久一区二区三区超碰国产精品| 欧美成人在线网站| 一本一本久久a久久精品牛牛影视| 欧美激情一区二区| 亚洲婷婷综合色高清在线| 欧美在线91| 亚洲福利视频在线| 欧美日韩一区二区在线播放| 亚洲影院高清在线| 久久亚洲精品欧美| 亚洲精品五月天| 美女精品在线观看| av成人福利| 久久青草欧美一区二区三区| 亚洲日本成人女熟在线观看| 国产精品夫妻自拍| 久久久久中文| 亚洲午夜91| 欧美韩国日本一区| 性欧美video另类hd性玩具| 精品999在线播放| 欧美视频一区二区| 久久亚洲视频| 亚洲在线国产日韩欧美| 欧美电影免费| 久久成人资源| 亚洲午夜在线视频| 亚洲国产三级在线| 国产日产欧美精品| 欧美日韩激情网| 麻豆久久精品| 欧美一区=区| 中文在线不卡视频| 久久精品91| 欧美四级伦理在线| 久久精品国产免费看久久精品| 亚洲激情在线激情| 国产日产亚洲精品| 欧美视频网址| 欧美精品激情blacked18|