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

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

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

 

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


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導航

統計

公告

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>
            99精品久久免费看蜜臀剧情介绍| 91久久精品日日躁夜夜躁欧美| 一本久道久久久| 亚洲三级国产| 欧美激情视频一区二区三区免费 | 久久精品国产亚洲一区二区三区| 国产美女一区二区| 久久久亚洲国产天美传媒修理工| 久久九九免费视频| 亚洲全黄一级网站| 亚洲视频高清| 国产一区二区三区久久悠悠色av | 亚洲综合精品四区| 激情亚洲成人| 亚洲精品社区| 国产区在线观看成人精品| 麻豆91精品| 欧美日韩一区二区在线视频| 亚欧美中日韩视频| 玖玖综合伊人| 午夜精品理论片| 两个人的视频www国产精品| 亚洲美女黄色片| 午夜精品福利在线| 亚洲精品久久视频| 午夜一区二区三视频在线观看| 亚洲成色777777女色窝| 妖精成人www高清在线观看| 国产亚洲激情视频在线| 91久久精品国产91久久| 国产亚洲精品自拍| 亚洲精品三级| 怡红院精品视频| 亚洲综合大片69999| 91久久精品国产91性色| 午夜亚洲影视| 亚洲一区二区三区久久| 欧美va天堂va视频va在线| 欧美在线观看视频在线| 欧美精品一卡二卡| 玖玖玖免费嫩草在线影院一区| 欧美调教vk| 亚洲国产婷婷| 国外成人性视频| 亚洲一区二区三区在线| 99国产成+人+综合+亚洲欧美| 欧美影视一区| 久久av最新网址| 国产精品国产三级国产| 亚洲人成绝费网站色www| 在线观看福利一区| 久久黄金**| 久久久久国内| 国产一区二区三区丝袜| 亚洲一区久久久| 亚洲一区二区欧美日韩| 欧美看片网站| 亚洲日韩欧美视频一区| 亚洲理伦电影| 欧美成人精品| 亚洲国产高清在线观看视频| 亚洲第一毛片| 美女免费视频一区| 欧美激情一区二区三区高清视频| 狠狠色综合网| 久久女同互慰一区二区三区| 久久久久久9| 一色屋精品视频在线看| 久久九九99| 美国十次了思思久久精品导航| 国产一区二区三区视频在线观看 | 欧美成人一区二区三区| 在线成人av网站| 久久免费视频网站| 欧美成熟视频| 99精品免费网| 欧美午夜在线视频| 亚洲欧美日韩爽爽影院| 久久久99精品免费观看不卡| 国产一区二区三区精品久久久 | 欧美国产日本| 妖精成人www高清在线观看| 欧美精品一级| 亚洲一区二区三区免费视频| 久久久精品视频成人| 伊人久久婷婷| 欧美激情精品久久久久久黑人| 亚洲欧洲一区二区三区| 亚洲一区综合| 韩国一区二区三区在线观看| 免费av成人在线| 一本到高清视频免费精品| 欧美亚洲日本一区| 亚洲高清三级视频| 欧美日韩综合| 久久久久久亚洲精品不卡4k岛国| 亚洲国产成人久久综合一区| 亚洲午夜激情免费视频| 国产一区二区三区日韩| 欧美aaa级| 亚洲欧美一区二区精品久久久| 免费黄网站欧美| 宅男66日本亚洲欧美视频| 国产亚洲欧美日韩精品| 欧美成人免费一级人片100| 亚洲午夜精品一区二区三区他趣| 久久理论片午夜琪琪电影网| 一区二区成人精品| 激情成人av在线| 欧美性色综合| 免费亚洲一区| 欧美一区二区三区喷汁尤物| 亚洲精品乱码久久久久久久久| 久久精品成人欧美大片古装| 日韩视频一区二区三区在线播放免费观看 | 亚洲国产天堂久久综合网| 欧美呦呦网站| 亚洲四色影视在线观看| 国产主播一区| 国产精品第13页| 欧美国产激情二区三区| 久久国产加勒比精品无码| 在线视频欧美一区| 亚洲激精日韩激精欧美精品| 久久视频在线看| 午夜国产精品视频免费体验区| 亚洲精品在线视频观看| 一区精品在线| 狠狠色丁香婷综合久久| 国产欧美韩日| 国产精品久久久久久超碰 | 久久国产精品一区二区三区四区| 中文亚洲字幕| 一本色道久久综合亚洲精品小说| 欧美福利影院| 麻豆精品视频| 噜噜噜91成人网| 久久视频国产精品免费视频在线 | 亚洲欧洲一二三| 亚洲国产乱码最新视频| 韩国一区二区三区在线观看| 国产亚洲美州欧州综合国| 国产欧美一区二区精品性色| 国产精品高潮视频| 国产精品久久久久久久久久久久| 欧美日韩免费观看中文| 欧美日韩精品系列| 欧美日韩精品综合在线| 欧美日韩在线一区| 欧美香蕉视频| 国产精品三区www17con| 国产精品一区在线观看你懂的| 国产精品青草久久| 国产麻豆成人精品| 国产一区二区三区黄| 伊人久久大香线蕉av超碰演员| 精品动漫一区二区| 亚洲欧洲精品天堂一级| 一区二区成人精品| 午夜精品短视频| 久久久久久久一区二区| 男人的天堂亚洲| 亚洲黄色三级| 制服丝袜亚洲播放| 欧美亚洲一区在线| 美女视频网站黄色亚洲| 欧美日本精品在线| 国产精品亚洲综合久久| 黄色欧美日韩| 一本色道婷婷久久欧美| 午夜精品在线看| 欧美jizzhd精品欧美巨大免费| 亚洲人成网站777色婷婷| 一区二区三区色| 久久精品一本久久99精品| 欧美国产一区视频在线观看| 国产精品久久福利| 在线高清一区| 亚洲女与黑人做爰| 免费观看不卡av| 一区二区三区四区五区视频| 久久精品国产99国产精品澳门| 欧美 日韩 国产一区二区在线视频| 欧美日韩一区二区免费在线观看| 国产午夜亚洲精品理论片色戒| 亚洲二区视频在线| 亚洲欧洲av一区二区| 欧美激情免费在线| 亚洲欧美在线高清| 欧美人妖另类| 在线精品视频在线观看高清| 亚洲网友自拍| 欧美v亚洲v综合ⅴ国产v| 亚洲一区二区高清视频| 免费黄网站欧美| 韩国精品主播一区二区在线观看| 夜夜嗨av一区二区三区网页| 老司机aⅴ在线精品导航| 在线一区二区三区四区| 欧美高清日韩|