C + + template-specific and biased

Source: Internet
Author: User


/*************************************************** 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 &AMP;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
*/







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.