---- 整理自狄泰软件唐佐林老师课程
类模板可以定义 任意多个不同的 类型参数
模板本来是一组通用逻辑的实现,但是可能存在特定的参数类型下,通用的逻辑实现不能满足要求,这时就需要针对这些特殊的类型,而实现一个特例模板——即 模板特化。
// 主模板类
template <typename T1, typename T2>
class Test
{
};
// 完全特化
template < >
class Test<int, int>
{
};
// 部分特化:对部分模板参数进行特化为一般类型
template <typename T2>
class Test<int, T2>
{
};
// 部分特化:模板参数特化为指针
template <typename T1, typename T2>
class Test<T1*, T2*>
{
};
#include
#include
using namespace std;
template
< typename T1, typename T2 >
class Test
{
public:
void add(T1 a, T2 b)
{
cout << "void add(T1 a, T2 b)" << endl;
cout << a + b << endl;
}
};
template
< typename T1, typename T2 >
class Test < T1*, T2* > // 关于指针的特化实现
{
public:
void add(T1* a, T2* b)
{
cout << "void add(T1* a, T2* b)" << endl;
cout << *a + *b << endl;
}
};
template
< typename T >
class Test < T, T > // 当 Test 类模板的两个类型参数完全相同时,使用这个实现
{
public:
void add(T a, T b)
{
cout << "void add(T a, T b)" << endl;
cout << a + b << endl;
}
void print()
{
cout << "class Test < T, T >" << endl;
}
};
template
< >
class Test < void*, void* > // 当 T1 == void* 并且 T2 == void* 时
{
public:
void add(void* a, void* b)
{
cout << "void add(void* a, void* b)" << endl;
cout << "Error to add void* param..." << endl;
}
};
int main()
{
Test<int, float> t1;
Test<long, long> t2;
Test<void*, void*> t3;
t1.add(1, 2.5);
t2.add(5, 5);
t2.print();
t3.add(NULL, NULL);
Test<int*, double*> t4;
int a = 1;
double b = 0.1;
t4.add(&a, &b);
return 0;
}
类模板特化与重定义有区别吗?函数模板可以特化吗?
部分内容引用修改自:
https://blog.csdn.net/zhaominyong/article/details/109265862
// 常规函数
bool Equal(const char* left, const char* right)
{
}
// 函数主模板
template<typename T1, typename T2>
bool Equal(T1 left, T2 right)
{
}
// 函数模板的完全特化
template< >
bool Equal<void*>(void* left, void* right)
{
}
// 函数模板的重载(新的函数模板)
template<typename T1, typename T2>
bool Equal(T1* left, T2* right)
{
}
#include
using namespace std;
// 函数主模板
template<typename T1, typename T2>
void Equal(T1 left, T2 right)
{
cout << "template void Equal(T1 left, T2 right)" << endl;
}
// 函数模板的完全特化
template< >
void Equal(const char* left, const char* right)
{
cout << "template< > void Equal(const char* left, const char* right)" << endl;
}
// 函数模板的重载(新的函数模板)
template<typename T1, typename T2>
void Equal(T1* left, T2* right)
{
cout << "template void Equal(T1* left, T2* right)" << endl;
}
int main()
{
Equal("1", "2");
return 0;
}
#include
using namespace std;
// 函数主模板
template<typename T1, typename T2>
void Equal(T1 left, T2 right)
{
cout << "template void Equal(T1 left, T2 right)" << endl;
}
// 函数模板的重载(新的函数模板)
template<typename T1, typename T2>
void Equal(T1* left, T2* right)
{
cout << "template void Equal(T1* left, T2* right)" << endl;
}
// 函数模板的完全特化
template< >
void Equal(const char* left, const char* right)
{
cout << "template< > void Equal(const char* left, const char* right)" << endl;
}
int main()
{
Equal("1", "2");
return 0;
}
#include
using namespace std;
// 函数主模板
template<typename T1, typename T2>
void Equal(T1 left, T2 right)
{
cout << "template void Equal(T1 left, T2 right)" << endl;
}
// 函数模板的完全特化
template< >
void Equal(const char* left, const char* right)
{
cout << "template< > void Equal(const char* left, const char* right)" << endl;
}
// 函数模板的重载(新的函数模板)
template<typename T1, typename T2>
void Equal(T1* left, T2* right)
{
cout << "template void Equal(T1* left, T2* right)" << endl;
}
// 普通函数
void Equal(const char* left, const char* right)
{
cout << "void Equal(const char* left, const char* right)" << endl;
}
int main()
{
Equal("1", "2");
return 0;
}
#include
#include
using namespace std;
template
< typename T1, typename T2 >
class Test
{
public:
void add(T1 a, T2 b)
{
cout << "void add(T1 a, T2 b)" << endl;
cout << a + b << endl;
}
};
/*
template
< >
class Test < void*, void* > // 当 T1 == void* 并且 T2 == void* 时
{
public:
void add(void* a, void* b)
{
cout << "void add(void* a, void* b)" << endl;
cout << "Error to add void* param..." << endl;
}
};
*/
class Test_Void
{
public:
void add(void* a, void* b)
{
cout << "void add(void* a, void* b)" << endl;
cout << "Error to add void* param..." << endl;
}
};
template < typename T >
bool Equal(T a, T b)
{
cout << "bool Equal(T a, T b)" << endl;
return a == b;
}
template < >
bool Equal<double>(double a, double b)
{
const double delta = 0.00000000000001;
double r = a - b;
cout << "bool Equal(double a, double b)" << endl;
return (-delta < r) && (r < delta);
}
bool Equal(double a, double b)
{
const double delta = 0.00000000000001;
double r = a - b;
cout << "bool Equal(double a, double b)" << endl;
return (-delta < r) && (r < delta);
}
int main()
{
cout << Equal( 1, 1 ) << endl;
cout << Equal<>( 0.001, 0.001 ) << endl;
return 0;
}