模板特化
介绍
- 模板特化是指在模板的基础上,针对某些特定的值,提供一种特殊的实现方式,模板特化分为两种,类模板特化和函数模板特化
类模板特化
- 类模板特化指的是在类模板上,针对某些特定的类型和值,提供一种特殊的实现方式。类模板特化分为全特化和偏特化两种。
类模板全特化
- 定义
- 示例
#include<iostream>
using namespace std;
template <typename T>
class MyType
{
public:
void print(){
cout << "This is a generic type." << endl;
}
};
template <>
class MyType<int>
{
public:
void print(){
cout << "This is an integer type" << endl;
}
};
int main()
{
MyType<float> f;
f.print();
MyType<int> i;
i.print();
return 0;
}
类模板偏特化
- 定义
- 示例
#include<iostream>
using namespace std;
template <typename T1, typename T2>
class MyType
{
public:
void print(){
cout << "This is a generic type" << endl;
}
};
template <typename T>
class MyType<T,int>
{
private:
public:
void print(){
cout << "This is a type with an interger as its second parmeter." << endl;
}
};
int main()
{
MyType<float, double> f;
f.print();
MyType<int,int> i;
i.print();
return 0;
}
函数模板特化
- 函数模板特化指的是在函数模板的基础上,针对某些特定的类型或值,提供一种特殊的实现方式。函数模板特化分为全特化和偏特化两种:
函数模板全特化
- 定义
- 示例
#include<iostream>
using namespace std;
template<typename T>
void MySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
template <>
void MySwap(double& a, double & b)
{
cout << "This is a special implementation for double types." << endl;
double temp = a;
a = b;
b = temp;
}
int main()
{
int x = 5, y = 10;
MySwap(x,y);
cout <<"X : " << x << " Y : " << y <<endl;
double p = 3.14, q = 6.28;
MySwap(p, q);
cout << "P : " << p << " Q : " << q << endl;
return 0;
}
函数模板偏特化
- 定义
- 示例
#include <iostream>
using namespace std;
template<typename A, typename B>
void f(A a, B b)
{
cout << "Normal Version" << endl;
}
int main()
{
int a = 10;
double b = 12;
f(a,b);
f(a,a);
return 0;
}
#include <iostream>
using namespace std;
template <typename A, typename B>
class F
{
public:
F(A a, B b):a_(a), b_(b){}
void operator() (){
cout << "Normal version" << endl;
}
private:
A a_;
B b_;
};
template <typename A>
class F<A,int>
{
public:
F(A a, int b) : a_(a), b_(b){}
void operator()(){
cout << "Partial version." << endl;
}
private:
A a_;
int b_;
};
int main()
{
int a = 10;
double b = 12;
F<int, double>(a,b)();
F<int,int>(a,a)();
return 0;
}
#include<iostream>
using namespace std;
struct NormalVersionTag{};
struct IntPartialVersionTag{};
template<class T>
struct TagDispatchTrait
{
using Tag = NormalVersionTag;
};
template<>
struct TagDispatchTrait<int>
{
using Tag = IntPartialVersionTag;
};
template <typename A, typename B>
inline void internal_f(A a, B b, NormalVersionTag)
{
cout << "Normal Version" << endl;
}
template <typename A, typename B>
inline void internal_f(A a, B b, IntPartialVersionTag)
{
cout << "Partial Version" << endl;
}
template <typename A, typename B>
void f(A a, B b)
{
return internal_f(a, b, typename TagDispatchTrait<B>::Tag {});
}
int main()
{
int a = 10;
double b = 12;
f(a,b);
f(a,a);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-10-09 多级指针使用分割字符串
2019-10-09 BMP图像信息隐藏