/********************************************************************
file name : CLK.h
author : Clark/陈泽丹
created : 2011-11-10
purpose : 常数当类型用,特化的函数与模板函数的优先级
通过template<int v>使数值能构成一个偏特化的类型, 这个特性会让很用ID作标示符的功能更方便,例如onTimer的ID
测试发现特化的函数比模板函数的优先级高
对于double参数时,Fun(double)比template<class T> void Fun(T)高
*********************************************************************/
#include <iostream>
using namespace std;
template<int v>
class Int2Type
{
enum {VALUE = v};
};
template<int TYPE_ID>
class Test
{
public:
void DoSomething()
{
DoSomething(Int2Type<TYPE_ID>());
}
template<class T>
void Fun(T)
{
cout<<"模板函数"<<endl;
}
void Fun(double)
{
cout<<"double函数"<<endl;
}
private:
void DoSomething(Int2Type<1>)
{
cout<<"DoSomething 1"<<endl;
}
void DoSomething(Int2Type<2>)
{
cout<<"DoSomething 2"<<endl;
}
template<class T>
void DoSomething(T)
{
cout<<"DoSomething other"<<endl;
}
};
void main()
{
Test<3> show;
show.DoSomething();
show.Fun('a');
show.Fun(5.0);
system("pause");
}
619

被折叠的 条评论
为什么被折叠?



