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

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

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

 

"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>
            精品999在线播放| 日韩天天综合| 亚洲理论在线| 亚洲精品影院| 一区二区免费在线视频| 亚洲精品一区在线观看香蕉| 亚洲视频电影图片偷拍一区| 亚洲欧美日韩第一区| 欧美在线网站| 久久伊人一区二区| 欧美福利在线观看| 日韩一区二区高清| 亚洲在线一区二区三区| 久久精品国产成人| 久久综合一区| 欧美激情综合在线| 国产精品红桃| 亚洲国产天堂久久国产91| 亚洲最新中文字幕| 久久九九免费| 欧美二区视频| 一本到12不卡视频在线dvd| 亚洲欧美日韩电影| 久久久久久婷| 欧美日韩小视频| 国产亚洲在线| 一区二区三区日韩精品视频| 久久久久国色av免费观看性色| 免费影视亚洲| 一本色道久久综合亚洲精品婷婷| 欧美在线关看| 欧美日韩精品不卡| 在线播放不卡| 亚洲一区二区三区免费观看| 免费亚洲一区| 午夜在线精品偷拍| 欧美人牲a欧美精品| 影院欧美亚洲| 久久大逼视频| 亚洲第一二三四五区| 91久久在线| 欧美精品一区二区高清在线观看| 欧美在线视频免费观看| 亚洲欧洲在线一区| 欧美在线播放一区| 国产精品国产精品国产专区不蜜| 最新高清无码专区| 老牛影视一区二区三区| 亚洲伊人网站| 国产精品www色诱视频| 亚洲国产欧美另类丝袜| 久久激情一区| 亚洲一区二区影院| 欧美精品在线网站| 亚洲电影在线看| 久久婷婷综合激情| 亚洲欧美精品在线| 国产精品美女主播| 亚洲国产毛片完整版 | 欧美日韩精品福利| 亚洲日本欧美| aa亚洲婷婷| 欧美精品九九| 国内精品久久久久久久97牛牛| 久久久视频精品| 亚洲国产另类久久精品| 国产精品麻豆欧美日韩ww| 欧美亚日韩国产aⅴ精品中极品| 美女视频一区免费观看| 欧美影视一区| 欧美在线视频不卡| 女仆av观看一区| 激情文学一区| 一区二区三区产品免费精品久久75| 最新成人av网站| 久久久中精品2020中文| 久久免费国产精品1| 欧美精品久久久久久久| 亚洲国产精品热久久| 欧美日韩一视频区二区| 欧美成人一区二区| 欧美在线观看www| 国产精品一区二区在线观看不卡| 欧美粗暴jizz性欧美20| 影音先锋亚洲精品| 久久人91精品久久久久久不卡| 黄色成人免费网站| 99re热精品| 亚洲一区二区免费视频| 欧美日韩在线视频一区| 欧美日韩免费精品| 亚洲国产成人久久综合| 亚洲成人在线视频播放| 欧美精品一区在线发布| 一区二区三区国产精华| 午夜日韩av| 欧美国产成人精品| 亚洲精品视频在线| 国产精品美女久久久久久久 | 国产综合网站| 国产欧美精品一区aⅴ影院| 亚洲国产影院| 欧美一区日本一区韩国一区| 伊人成人开心激情综合网| 免费在线观看一区二区| 亚洲午夜在线视频| 欧美国产日韩精品免费观看| 久久久国产精品亚洲一区 | 欧美精品www在线观看| 欧美日韩亚洲三区| 久久视频这里只有精品| 国产精品一区二区你懂的| 亚洲午夜激情网站| 欧美韩日亚洲| 欧美一级久久久久久久大片| 欧美一级理论片| 亚洲视频精品在线| 亚洲一级一区| 99国产精品99久久久久久粉嫩| 欧美亚洲三级| 国产欧美日韩一区| 国产老女人精品毛片久久| 亚洲二区在线视频| 在线成人www免费观看视频| 一本久久a久久精品亚洲| 宅男噜噜噜66一区二区66| 久久精品国产精品 | 国产情侣久久| 欧美黑人一区二区三区| 欧美激情性爽国产精品17p| 欧美日韩另类在线| 久久黄色网页| 欧美成人精品三级在线观看| 午夜精品久久久久久久久| 亚洲一级在线观看| 国模私拍一区二区三区| 亚洲国产一二三| 亚洲国产精品美女| 欧美在线视频观看| 欧美在线观看视频| 国产精品www| 国产精品99久久久久久白浆小说| 中文日韩欧美| 国产精品国产三级国产普通话三级| 91久久在线| 日韩视频一区二区在线观看 | 玖玖玖国产精品| 欧美高清影院| 欧美aa国产视频| 亚洲黄色免费| 欧美精品一区三区在线观看| 亚洲第一在线综合网站| 亚洲人成77777在线观看网| 欧美国产日韩一区二区在线观看| 久久综合色影院| 亚洲精品日韩综合观看成人91| 久久免费国产精品1| 久久成人这里只有精品| 国产中文一区| 久久精品亚洲精品| 欧美成人福利视频| 亚洲区一区二| 欧美日韩精品三区| 一本色道久久综合亚洲精品高清| 性欧美8khd高清极品| 亚洲午夜在线| 亚洲自拍偷拍色片视频| 国产欧美日韩综合一区在线播放| 午夜免费电影一区在线观看| 久久久www| 99xxxx成人网| 国产亚洲精品久久久久久| 理论片一区二区在线| 亚洲美女色禁图| 先锋资源久久| 91久久久国产精品| 欧美日韩大片| 午夜一级久久| 91久久在线| 亚洲一区日韩| 1024亚洲| 欧美精品啪啪| 久久精品一二三| 亚洲午夜伦理| 亚洲国产日韩一区| 久久久精彩视频| 亚洲午夜视频在线| 亚洲第一黄色网| 国产欧美一区二区三区沐欲| 欧美片在线观看| 久久久噜噜噜久久久| 亚洲一本大道在线| 欧美 日韩 国产一区二区在线视频 | 久久婷婷久久| 在线一区二区三区四区| 狠狠色2019综合网| 国产精品欧美日韩一区二区| 欧美h视频在线| 久久精品道一区二区三区| 一区二区欧美亚洲|