緇堜簬鏃犺亰鍒版潵鍐欎功璇勶紝鏈榪戠殑欏圭洰涓鐩撮兘娌″拰C++鏈変粈涔堝叧緋伙紝涓嶈繃鐪嬬殑涔﹀嵈閮芥槸C++鏂歸潰鐨勶紝鑰屾渶榪戠湅鍒扮殑鍑犳湰涔︿腑鎰熻鏈濂界殑鑾繃浜庤繖鏈奀++ Templates銆?BR>
Nicolai M. Josuttis鐨勪功鎴戝緢鍠滄錛屼粠浠栫殑閭f湰銆奣he C++ Standard Template Library銆嬪氨鐪嬪嚭浜嗕粬寰堝鐙壒鐨勯鏍鹼紝浠ゆ垜鐖變笉閲婃墜錛屾墍浠ヨ繖鏈奀++ Template銆?nbsp; 涔熻繘鍏ヤ簡鎴戠殑蹇呯湅涔﹀崟銆傜矖璇諱箣鍚庯紝鎰熻鏁存湰涔︾粷瀵瑰皢鎴愪負C++娉涘瀷棰嗗煙鐨勫湥緇忕駭钁椾綔
- 榪欐湰涔﹁搴﹂夊緱寰堝ソ錛屽叏涔﹀垎涓変釜閮ㄥ垎錛屽垎鍒粙緇嶆ā鏉垮熀紜銆佹ā鐗堢殑緙栬瘧鍣ㄥ疄鐜般佹ā鏉跨殑楂樼駭鎶宸э紝涓変釜閮ㄥ垎鐩歌緟鐩告垚銆佺浉浜掔収搴旓紝鐢辨祬鍏ユ繁鑰屽張鑷劧鑰岀劧錛岃繕鏂逛究鍒嗗紑闃呰錛堟瘮濡傛垜灝遍噸鐐圭湅浜嗙涓絎笁閮ㄥ垎錛屾ā鐗堝疄鐜拌鎴戠暐榪囦簡
錛夊嵈鍙堝叏闈㈣鐩栦簡榪欎竴棰嗗煙
- 榪欐湰涔﹁嫳鏂囧緢嫻呮樉錛堟瘮銆奙odern C++ Design銆嬫祬鏄句簡涓嶇煡澶氬皯鍊嶏級錛岃璦涓ヨ皚鑰屽張涓嶆櫐娑╋紝灝ゅ叾瑕佽禐鐨勫氨鏄簾璇濆挨鍏跺湴灝戯紒
- 绔犺妭瀹夋帓寰堝悎鐞嗭紝寰堟柟鍒綔涓哄伐鍏蜂功搴旀ユ煡闃咃紙銆奀++STL銆嬪氨鏈夎繖涓紭鐐癸紝涓庤繖鏈功縐戝瀹?宸ョ▼甯堢殑緇勫悎涓嶆棤鍏崇郴錛?BR>
- 涔︿腑濂藉鎶鏈紝鎴戞槸闂繪墍鏈椈錛屾儕涓哄ぉ浜猴紝灝ゅ叾絎笁閮ㄥ垎錛屽彲浠ョ畻寰椾笂鐪艱姳緙貢錛岃屼笖緇欏嚭鐨勫疄鐜版劅瑙夋棦絎﹀悎鏍囧噯銆佸疄鐢ㄣ佽屼笖娌℃湁鐐妧鐨勬垚鍒?/LI>
鍚岀被涔︾睄鎹垜鎵鐭ユ病鏈夊彲浠ヨ揪鍒拌繖涓珮搴︾殑錛屽ぇ閮ㄥ垎C++娉涘瀷鏂歸潰鐨勪笓钁楀彧灞闄愪簬鎬庝箞鐢⊿TL錛屽皢妯℃澘鍩虹鐨勪功錛屼篃浠呴檺浜庢渶琛ㄩ潰鐨勮娉曪紝鍍忔ā鐗堝弬鏁版帹瀵艱繖縐嶉棶棰橀矞鏈夋秹鍙婏紝鏇翠笉鐢ㄦ彁鍏充簬Metaprogramming錛岃繖鏈功鍦g粡鐨勫湴浣嶄及璁″悗浜轟篃鏄毦浠ヤ紒鍙婁簡銆?/P>
涓嬮潰鏄垜鐪嬩功鏃剁敾涓嬫潵鐨勪竴浜涜寰楄嚜宸卞鉤鏃跺簲璇ユ敞鎰忕殑鍦版柟錛屾斁鍦ㄨ繖閲屽仛澶囧繕濂戒簡
- (P12) [Argument Deducion] If we pass two ints to the parameter type T const& the C++ compiler must conclude that T must be int. Note that no automatic type conversion is allowed here,Each T must match exactly.
template <typename T>
inline T const& max (T const& a,T const& b);


max(4,7)//OK:T is int for both arguments
max(4,4.2)//ERROR:first T is int,second T is double
- (P13)[Template Parameters] In function templates(unlike class template) no default template arguments can be specified
- (P14)[Template Parameters]Deducation can be seen as part of overlaod resolution-a process tha is not based on selection of return type either.The sole exception is the return type of conversion operator members.
- (P18)[Overloading Function Template] The fact that not all overloaded functions are visible when a corresponding function call is made may or may not matter.
- (P39)[Nontype Function Template Parameters] Function templates are considered to name a set of overloaded function.However,according to the current standard,sets of overload functions cannot be used for template parameter deducation.Thus you have to cast to the exactly type of the function template arguments
template <typename T,int VAL>
T addValue (T const& x)


{
return x+VAL
}

std::transform(source.begin(),source.end(),//start and end of source
dest.begin(),//start of destination
(int(*)(int const&))addValue<int,5>);//operation

- (P40)[Restrictions for Nontype Template Parameters] 澶暱浜嗭紝鐣ヨ繃
- (P44)[The .template Construct]
template <int N>
void printBitset (std::bitset<N> const& bs)


{
std::cout<<bs.to_string<char,char_traits<char>,allacator<char> >();//ERROR:can't recogonize the template
}

template <int N>
void printBitset (std::bitset<N> const& bs)


{
std::cout<<bs.template to_string<char,char_traits<char>,allacator<char> >();//OK
}
- (P45)[Using this->]
template <typename T>
class Base


{
public:
void bar();
};

template <typename T>
class Derived : Base<T>


{
public:
void foo()

{
bar();//call external bar() or error
}
}

template <typename T>
class Derived : Base<T>


{
public:
void foo()

{
this->bar();//OK
}
}
- 鍚屾牱綺懼僵鐨勮繕鏈?P57)[Using String Literals as Arguments for Function Templates]
- 浠ゆ垜鎯婂紓鐨凷FINE鎶鏈?substitution-failure-is-not-an-error)
template <typename T>
class IsClassT


{
private:
typedef char One;

typedef struct
{char a[2];} Two;
template <typename C> static One test (int::C*);
template <typename C> static Two test(
);
public:

enum
{Yes=sizeof(IsClassT<T>::test<T>(0))==1};

enum
{No=!Yes};
};
鎬昏岃█涔嬶紝姝や功甯︾粰浜嗘垜鍓嶆墍鏈湁鐨勯槄璇諱韓鍙?.....鎴戜粖騫撮渿鎾煎ぇ濂栦竴瀹氫細鎶曞畠涓紲?img src ="http://www.shnenglu.com/windreamer/aggbug/1657.html" width = "1" height = "1" />
]]>