/*************************************************** 1. Template specificity is divided into two types by object type (class and global function): The special of class template and the special of global template function;
2. The specificity of the type of the total specificity and specificity (that is, multiple template parameters can be selected only one or more), the global template function of the specificity does not support the partial specificity;
3. The special of the global template function needs to pay attention to several important elements function return value function name parameter type, number and order, eg:
Template <class t> t max_ (const T, Const T &)
4. The special of class template needs to pay attention to several important element class name, parameter type and number, the order of formal parameter is unimportant;
****************************************************/
#include <string>
#include <iostream>
#include <cstring>
using namespace Std;
Template <class t> t max_ (const T &a, const T &b)
{
Return a > B? A:B;
}
The type and order of the fully-specific parameters are required
Template <> Const char* MAX_ (const char* const &a,const char* const &B)
{
Return strcmp (a,b) >= 0? A:B;
}
Template <class t,class p> struct A
{
A (const T &a,const P &b)
{
std::cout<< "class template not specific," << "A:" <<a<< ", B:" <<b<<endl;
}
};
Template <> struct A <const char *,int>
{
A (const char *a,int b)
{
std::cout<< "class template fully-specific," << "A:" <<a<< ", B:" <<b<<endl;
}
};
Template <class t> struct A <const t&,int>
{
A (const t& A,int b)
{
std::cout<< "class template biased," << A: "<<a<<", B: "<<b<<endl;
}
};
int main (int argc,char *argv[])
{
int a = 111,b = 222;
Std::cout<<max_ (a,b) <<endl;
const char * p1 = "AAA", * p2= "BBB";
Std::cout<<max_ (P1,P2) <<endl;
std::string S1 = "CCC", S2 = "ddd";
Std::cout<<max_ (S1,S2) <<endl;
A<int,int> A1 (A,B);
A<const Char *,int> A2 (P1,B);
A<const std::string&,int> A3 (S1,B);
return 0;
}
/*
[Root@six src]# g++ test.cpp-g-o test;. /test
222
Bbb
Ddd
Class template is not special, a:111,b:222
Class Template a:aaa,b:222,
Class template biased, a:ccc,b:222
*/