#include <cstdlib>
#include <iostream>
using namespace std;
template<int v>
struct Int2Type
{
enum{value = v};
};
void Do(Int2Type<0>* t)
{
cout<<"0:"<<t->value<<endl;
delete t;
};
void Do(Int2Type<1>* t)
{
cout<<"1:"<<t->value<<endl;
delete t;
};
void Do(Int2Type<2>* t)
{
cout<<"2:"<<t->value<<endl;
delete t;
};
int main(int argc, char *argv[])
{
Do(new Int2Type<0>);
Do(new Int2Type<1>);
Do(new Int2Type<2>);
system("PAUSE");
return EXIT_SUCCESS;
}
本文展示了一个使用C++模板元编程的例子,通过定义特定类型的结构体并实现多态的Do函数来输出不同整数值。该程序利用了模板特化来区分不同的整数,并通过指针调用相应的函数。
7121

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



