锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
//聽knapSackSupportTest.cpp聽:聽瀹氫箟鎺у埗鍙板簲鐢ㄧ▼搴忕殑鍏ュ彛鐐廣?br />
//
#include聽"stdafx.h"
typedef聽__int64聽DT;
template<int聽N>
struct聽factorial
{
聽聽聽聽static聽const聽DT聽value聽=聽N聽*聽factorial<N-1>::value;
};
template<>
struct聽factorial<1>
{
聽聽聽聽static聽const聽DT聽value聽=聽1;
};
static聽DT聽arrFactorial[32];
int聽_tmain(int聽argc,聽_TCHAR*聽argv[])

{
聽聽聽聽arrFactorial[1]聽=聽factorial<1>().value;
聽聽聽聽arrFactorial[2]聽=聽factorial<2>().value;
聽聽聽聽arrFactorial[3]聽=聽factorial<3>().value;
聽聽聽聽arrFactorial[4]聽=聽factorial<4>().value;
聽聽聽聽arrFactorial[5]聽=聽factorial<5>().value;
聽聽聽聽arrFactorial[6]聽=聽factorial<6>().value;
聽聽聽聽arrFactorial[7]聽=聽factorial<7>().value;
聽聽聽聽arrFactorial[8]聽=聽factorial<8>().value;
聽聽聽聽arrFactorial[9]聽=聽factorial<9>().value;
聽聽聽聽arrFactorial[10]聽=聽factorial<10>().value;
聽聽聽聽arrFactorial[11]聽=聽factorial<11>().value;
聽聽聽聽arrFactorial[12]聽=聽factorial<12>().value;
聽聽聽聽arrFactorial[13]聽=聽factorial<13>().value;
聽聽聽聽arrFactorial[14]聽=聽factorial<14>().value;
聽聽聽聽arrFactorial[15]聽=聽factorial<15>().value;
聽聽聽聽arrFactorial[16]聽=聽factorial<16>().value;
聽聽聽聽arrFactorial[17]聽=聽factorial<17>().value;
聽聽聽聽
聽聽聽聽for(int聽i=1;i聽<聽18;i++)
聽聽聽聽
{
聽聽聽聽聽聽聽聽printf("factorial聽of聽%d=%u\n",i,arrFactorial[i]);
聽聽聽聽}聽聽聽聽
聽聽聽聽return聽0;
}

NullType鍙湁澹版槑娌℃湁瀹氫箟銆?/P>
class NullType;
EmptyType錛屽氨鏄竴涓┖綾?BR>

struct EmptyType
{};
榪欐槸鍙緇ф壙鐨勫悎娉曞瀷鍒紝鍙互浣滀負template鐨勭己鐪佸弬鏁板瀷鍒?img src ="http://m.shnenglu.com/tommy/aggbug/3081.html" width = "1" height = "1" />
涓嬮潰鐨勪唬鐮佸嚭鑷?Loki搴擄細
鎬誨緱鏉ヨ鏄彁渚涗簡std::type_info鐨勬墍鏈夋垚鍛樺嚱鏁幫紱
鎻愪緵浜唙alue璇箟錛屽嵆public copy鏋勯犲嚱鏁板拰public assignment鎿嶄綔絎︼紱
瀹氫箟浜?operator< 鍜?operator== 絳?BR>
namespace Loki

{
/**/////////////////////////////////////////////////////////////////////////////////
// class TypeInfo
// Purpose: offer a first-class, comparable wrapper over std::type_info
/**/////////////////////////////////////////////////////////////////////////////////
class TypeInfo
{
public:
// Constructors
TypeInfo(); // needed for containers
TypeInfo(const std::type_info&); // non-explicit
// Access for the wrapped std::type_info
const std::type_info& Get() const;
// Compatibility functions
bool before(const TypeInfo& rhs) const;
const char* name() const;
private:
const std::type_info* pInfo_;
};
// Implementation
inline TypeInfo::TypeInfo()
{
class Nil
{};
pInfo_ = &typeid(Nil);
assert(pInfo_);
}
inline TypeInfo::TypeInfo(const std::type_info& ti)
: pInfo_(&ti)
{ assert(pInfo_); }
inline bool TypeInfo::before(const TypeInfo& rhs) const
{
assert(pInfo_);
return pInfo_->before(*rhs.pInfo_) != 0;
}
inline const std::type_info& TypeInfo::Get() const
{
assert(pInfo_);
return *pInfo_;
}
inline const char* TypeInfo::name() const
{
assert(pInfo_);
return pInfo_->name();
}
// Comparison operators
inline bool operator==(const TypeInfo& lhs, const TypeInfo& rhs)
{ return (lhs.Get() == rhs.Get()) != 0; }
inline bool operator<(const TypeInfo& lhs, const TypeInfo& rhs)
{ return lhs.before(rhs); }
inline bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs)
{ return !(lhs == rhs); }
inline bool operator>(const TypeInfo& lhs, const TypeInfo& rhs)
{ return rhs < lhs; }
inline bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs)
{ return !(lhs > rhs); }
inline bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs)
{ return !(lhs < rhs); }
}
template <class T,class U>
class Conversion

{
//
public:
enum
{ exists2Way = exists && Conversion<U,T>::exists };
enum
{ sameType = false };
};
sameType 琛ㄧず T鍜孶鏄惁鍚屼竴涓被鍨嬨?BR>涓嶈繃錛岃櫧鐒朵功閲岃繖涔堣錛屾垜鎬庝箞閮芥悶涓嶆噦涓轟粈涔堣繖鏍峰彲浠ワ紝嫻嬭瘯涔熸槸涓嶅鐨勶紝闅鵑亾榪欎釜sameType鐨勫啓娉曡繕鏈夊埆鐨勫ゥ濡欙紵
涓嶈繃涓嬮潰榪欎釜鍋忕壒鐨勫啓娉曞掓槸姣旇緝瀹規槗鐞嗚В錛?BR>
template <class T>
class Conversion<T,T>

{
public:
enum
{ exists = 1,exists2Way = 1,sameType = 1 };
};
鏈変簡榪欏嚑涓父鏁幫紝瑕佸喅瀹氫袱涓猚lass涔嬮棿鏄惁瀛樺湪緇ф壙鍏崇郴灝辨瘮杈冨鏄撲簡錛?BR>
#define SUPERSUBCLASS(T,U) \
(Conversion<const U*, const T*>::exists && \
!Conversion<const T*, const void*>::sameType)
#define SUPERSUBCLASS_STRICT(T,U) \
(SUPERSUBCLASS(T,U) && \
!Conversion<const T, const U>::sameType)
鍙﹀錛屽姞涓?const 鏄負浜?闃叉鍥犱負 const 鑰屽鑷磋漿鍨嬪け璐ワ紝瀵逛簬宸茬粡鏄痗onst鐨勪笢瑗垮啀const涓嬈$殑璇濆悗闈竴嬈$殑const浼氬拷鐣ユ帀銆?BR>
鍐嶏紝榪欎釜瀹忕殑鍚嶅瓧寰堟竻鏅幫紝灝辨槸 瓚呯被--瀛愮被錛?鍓嶉潰閭d釜T鏄秴綾伙紝U鏄瓙綾伙紝榪欎釜鍛藉悕姣?INHERITS瑕佸ソ銆?img src ="http://m.shnenglu.com/tommy/aggbug/3079.html" width = "1" height = "1" />
template <class T,class U>
class Conversion

{
typedef char Small;
class Big
{ char dummy[2]; };
static Small Test(U);
static Big Test(
);
static T MakeT();
public:
enum
{ exists = sizeof(Test(MakeT())) == sizeof(Small) };
};
鍏充簬鍑芥暟 Small Test(U) 鍜?Big Test(...) 錛屼功閲岄潰璇達細
銆傘傘傞渶瑕佷袱涓噸杞藉嚱鏁幫紝鍏朵竴濡傚厛鍓嶆墍璇達紝鎺ュ彈涓涓猆瀵硅薄騫朵紶鍥炰竴涓猄mall瀵硅薄錛?BR>
Small Test(U);
浣嗘帴涓嬫潵錛屾垜璇ュ浣曞啓鍑轟竴涓彲鎺ュ彈浠諱綍鍏朵粬縐嶅璞$殑鍑芥暟鍛紵銆傘傘傘?/SPAN>
鎴戣寰楄繖涓湴鏂圭炕璇戝緱鏈夌偣闂錛屾槸涓嶆槸搴旇璇達細鈥溿傘傛垜璇ュ浣曞啓鍑轟竴涓彲鎺ュ彈浠諱綍鍙﹀涓縐嶇被鍨嬶紙鍗?T錛夌殑瀵硅薄鐨勫嚱鏁板憿銆傘傗濓紝鍥犱負榪欓噷灝辨槸 T鍜孶鍢涳紝 娌℃湁浠涔堚滃叾浠栫鈥濓紝榪欐牱緲昏瘧瀹規槗璁╂垜榪鋒儜涓嶈В浜嗕竴浼氬効銆?BR>
濡傛灉鎺ュ彈 U鐨勯偅涓嚱鏁拌璋冪敤錛屽垯T鍙互琚漿鎹負 U錛屽惁鍒欐棤娉曡漿鎹紝榪欎釜鏄濊礬鐨勬牴鏈?/FONT>
涓轟粈涔堣鍋氫竴涓?MakeT榪欐牱鐨勫嚱鏁拌屼笉鐩存帴浣跨敤T鍛紵 榪欐槸涓轟簡婊¤凍褰?T 鍙湁縐佹湁鏋勯犲嚱鏁扮殑鎯呭喌錛屽浜庣紪璇戝櫒鏉ヨ錛宻izeof 鏄湪緙栬瘧鏈熷畬鎴愯瘎浼扮殑錛屾墍浠ワ紝MakeT 閲岄潰鍒板簳鍋氫簡浠涔堝茍涓嶉噸瑕侊紝閲嶈鐨勬槸浠栬繑鍥炵殑綾誨瀷錛屾槸 T錛屾墍浠ワ紝浣滆呭緢鍏村鍦拌錛岃繖鏄竴涓?StrawMan function錛屽嵆鈥滅ɑ鑽変漢鍑芥暟鈥濓紝鍝堝搱錛屽彧鏄竴涓牱瀛愯屽凡錛屼絾鏄繖宸茬粡瓚沖浜嗭紝閭d袱涓噸杞界殑Test鏂規硶涔熸槸涓鏍鳳紝榪欓噷鎴戜滑涓嶅叧蹇冧粬鐨勫嚱鏁頒綋銆傚己鍟婏紝鐖芥姝紝蹇劅鐨勬簮娉夊晩
嫻嬭瘯浠g爜濡備笅錛?BR>
using namespace std;
cout << Conversion<double, int>::exists << ' '
<< Conversion<char, char*>::exists << ' '
<< Conversion<size_t, vector<int> >::exists << ' ';
double鍙互杞崲涓?int
char 涓嶈兘杞崲涓?char*
vector<int> 鏄竴涓鍣ㄧ殑鏋勯犲嚱鏁幫紝size_t 涓嶈兘杞崲錛屽洜涓鴻繖涓瀯閫犲嚱鏁版槸 explicit鐨?榪欎釜鍦版柟榪樻槸鏈夌偣榪風硦錛岃繕寰楃爺絀朵竴涓嬨?img src ="http://m.shnenglu.com/tommy/aggbug/3059.html" width = "1" height = "1" />
鏈夋椂鍊欙紝鑼冨瀷紼嬪簭闇瑕佹牴鎹竴涓猙oolean鍙橀噺鏉ラ夋嫨鏌愪釜鍨嬪埆鎴栧彟涓涓瀷鍒?BR>涓嬮潰瀹氫箟鐨勭粨鏋勬彁鍑轟簡瑙e喅鏂規錛?BR>
template <bool flag,typename T,typename U>
struct Select

{
typedef T Result;
};
//鍋忕壒鍖?/SPAN>
template <typename T,typename U>
struct Select<false,T,U>

{
typedef U Result;
};
鍋忕壒鍖栫湡寮猴紝鍏ㄥ湪涔庢兂璞″姏浜?img src ="http://m.shnenglu.com/tommy/aggbug/3057.html" width = "1" height = "1" />
template <typename T>
struct Type2Type

{
typedef T OriginalType;
};
template <class T,class U>
T* Create(const U& arg)

{
return new T(arg);
}
template <class U>
Widget* Create<Widget,U>(const U& arg)

{
return new Widget(arg,-1);
}
template <class T,class U>
T* Create(const U&arg,T /**//*铏氭嫙*/)

{
return new T(arg);
}
template <class U>
Widget * Create(const U& arg, Widget /**//*铏氭嫙*/)

{
return new Widget(arg,-1);
} 榪欐牱鏄彲浠ヨВ鍐抽棶棰橈紝浣嗘渶澶х殑姣涚梾鍦ㄤ簬榪愯鏃舵瀯閫犱簡 鏈浣跨敤鐨勫璞¤繖涓紑閿錛堣櫄鎷熺殑Widget鍙傛暟錛夈傝繖鏃?Type2Type 榪欎釜鍜氬挌鍑哄満浜嗭紝鎸夌収涔︾殑璇存硶錛岃繖鏄滀竴涓瀷鍒唬琛ㄧ墿銆佷竴涓彲浠ヨ浣犱紶緇欓噸杞藉嚱鏁扮殑杞婚噺綰D鈥濓紝濡備笅錛?BR>
template <class T,class U>
T* Create(const U& arg,Type2Type<T>)

{
return new T(arg);
}
template <class U>
Widget * Create(const U& arg,Type2Type<Widget>)

{
return new Widget(arg,-1);
}
璋冪敤鏂癸細
String *pStr = Create("hello",Type2Type<String>());
Widget *pW = Create(100,Type2Type<Widget>());
鍏抽敭鏄紝榪欎釜涓滆タ涔熸槸緇欑紪璇戝櫒鐪嬬殑錛屽
template <int v>
struct Int2Type

{
enum
{ value = v };
};
template <typename T, bool isPolimorphic>
class NiftyContainer

{
private:
void DoSomething( T* pObj, Int2Type<true>)
{
T* pNewObj = pObj->Clone();
}
void DoSomething( T* pObj, Int2Type<false>)
{
T * pNewObj = new T(*pObj);
}
public:
void DoSomething(T * pObj)
{
DoSomething(pObj, Int2Type<isPolimorphic>());
}
};
template <class T>
struct OpNewCreator

{
static T* Create()
{
return new T;
}
};
template <class T>
struct MallocCreator

{
static T* Create()
{
void * buf = std::malloc(sizeof(T));
if (! buf ) return 0;
return new(buf) T;
}
};
template <class T>
struct PrototypeCreator

{
PrototypeCreator(T* pObj = 0) : pPrototype_(pObj)
{}
T * Create()
{
return pPrototype_ ? pPrototype_->Clone() : 0;
}
T* GetPrototype()
{ return pPrototype_; }
void SetPrototype(T * pObj)
{ pPrototype_ = pObj; }
private:
T* pPrototype_;
};

class Widget

{
};

template <template <class Created> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>

{
//
};


int _tmain(int argc, _TCHAR* argv[])

{
typedef WidgetManager< OpNewCreator > myWidgetMgr;
return 0;
}