#include<iostream>
using namespace std;
template<class T>
struct CTest{
void operator()(){cout<<"CTest<T>/n";}
};
template<>
struct CTest<char>{
void operator()(){cout<<"CTest<char>/n";}
};
template<>
struct CTest<int>{
void operator()(){cout<<"CTest<int>/n";}
};
int main()
{
CTest<double> test1;
CTest<int> test2;
CTest<char> test3;
test1();
test2();
test3();
return 0;
}
运行输出:
CTest<T>
CTest<int>
CTest<char>
本文通过一个C++模板特化的具体实例,展示了如何为不同数据类型定制特定的行为。该示例定义了一个通用模板CTest,以及针对char和int类型的特化版本,并演示了如何调用这些特化版本。
440

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



