The C++ class template specialization and partial specialization
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Creating and using the C++ class template specialization and partial specialization in C++ programming
To show: How to create and use the C++ class template specialization and partial specialization in C++ programming
// C++ template specialization and partial specialization
#include <iostream>
using namespace std;
// general, for all types
template <class any_data_type>
any_data_type Test(any_data_type Var1)
{return Var1;}
// partial specialization for all pointers type
template <class any_data_type>
any_data_type * Test(any_data_type *Var2)
{return Var2;}
// specialization, just for const char *
template <>const char * Test(const char *Var3)
{return Var3;}
// do some testing
int main(void)
{
int p = 5;
// call Test(any_data_type)
int q = Test(p);
double r = Test(3.1234);
cout<<"General types = "<<q<<endl;
cout<<"General types = "<<r<<endl;
// call Test(any_data_type*)
int *s = Test(&p);
char *t = "Partial lor!";
cout<<"Partial types = "<<s<<endl;
cout<<"Partial types = "<<t<<endl;
// call Test(const char *)
const char *u = Test("Specialized!");
cout<<"Specialization type = "<<u<<endl;
return 0;
}
Output examples:
General types = 5
General types = 3.1234
Partial types = 0012FF60
Partial types = Partial lor!
Specialization type = Specialized!
Press any key to continue . . .