C ++程序中的模板专业化?
在本教程中,我们将讨论一个程序,以了解C++中的模板专业化。
诸如此类的标准函数sort()可用于任何数据类型,并且它们的行为相同。但是,如果您想为特定的数据类型(甚至是用户定义的)设置函数的特殊行为,我们可以使用模板专门化。
示例
#include <iostream>
using namespace std;
template <class T>
void fun(T a) {
cout << "The main template fun(): " << a << endl;
}
template<>
void fun(int a) {
cout << "Specialized Template for int type: " << a << endl;
}
int main(){
fun<char>('a');
fun<int>(10);
fun<float>(10.14);
return 0;
}输出结果
The main template fun(): a Specialized Template for int type: 10 The main template fun(): 10.14
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西