function modules, class templates

Source: Internet
Author: User
Tags strcmp

function template specificity

function template specificity:

    1. Keyword template followed by a pair of <>
    2. Then the template name and a pair of <>, and the angle brackets define the template parameter of this special wah
    3. function formal parameter list
    4. function body
 template <typename t>int  com (const  t &v1,const  T &v2) { return  v1>v2? 1 : 0  ;} Template  <>int  com<const  string  > (const  string  &v1,const  string  &v2) { return  Span style= "COLOR: #000000" > strcmp (V1.c_str (), V2.c_str ());}  

The declaration of specialization must match the corresponding template, and when the CMP function is called, it is passed to his two const string arguments, the compiler calls the special version, the specialization function parameter is fixed to the const string type, and when other types (including string) are called, the generalization version is called

1. Declaration of a special template

A function-specific template can be declared without having to define

Template <>int com<conststring> (conststring &v1,  Conststring &v2)

Attention:

    1. Template specificity always contains template null argument specifier (template<>)
    2. You must include a function parameter list, and if you can infer template arguments from function parameters, you do not have to display the definition template arguments
Template <>int com (conststring &v1,conststring &v2) {    return  strcmp (V1.c_str (), V2.c_str ());}
2. function overloading and template specificity

If TEMPLATE<> is omitted, then the overloaded non-template version of the function

Template <typename t>int com (const T &v1,const T &v2) {      Return V1>v2? 1:0;} // Template <> int com (conststring &v1,conststring &v2) {     return strcmp (V1.c_str (), V2.c_str ());}

Attention:

  When a non-template function is defined, a general transform is applied to the argument, and the corresponding argument type does not apply the transform when the template is special.

In a template-specific version of a call, the argument type must exactly match the function type parameter of the special version, and if it does not match exactly, the compiler instantiates an instance of the template definition for the argument.

3. Duplicate definitions cannot always be detected

You should include a template-specific declaration in a header file that contains the header file for each source file

Full code

#include <iostream>#include<cstring>using namespacestd;template<typename t>intcomConstT &v1,ConstT &v2) {    returnV1>v2?1:0;} Template<>intcomConst string&AMP;V1,Const string&v2) {    returnstrcmp (V1.c_str (), V2.c_str ());}intMain () {intA=1, b=2; strings1="123", s2="1"; cout<<com (S1,S2) <<Endl; return 0;}
Class template specificity

  You can define a member that differs from the template itself. If a particular member cannot be defined from a template, the special type cannot use that member. The definition of a class template is not used to display the definition for creating a special member.

1. Definition of class specificity

  Members are not template<> when defining members outside of a class-specific type

/* * class is specific to the const char* type, the above function is special like * This example is just to illustrate the class-  specific template definition members */ void queue<constchar*>::p ush (constChar *val) {     return  Real_queue.push (val);}
2. Special members and non-special categories
void queue<constchar*>::p ush (constChar *val);

As with any special function template, start with the formal parameter list of the null parameter, in the header file that defines the class

3. Partial localization of class templates
Template <class T1,class t2>class  some_templae{    //}  //template<t1>class some_template<t1,int>{    //}

Attention:

    1. Definition of partial Special Image class template
    2. A partially-customized template parameter is a subset of formal parameter tables defined by a class template
    3. Some of the taxiing parameters list only those arguments for the unknown template parameter
#include <iostream>#include<cstring>using namespacestd;template<classT1,classT2>classship{ Public: Ship (T1 a1,t2 A2): a (A1), B (A2) {}voidShow () {cout<<a<<" "<<b<<Endl; }    Private: T1 A; T2 b;};//The T2 is of type int and T1 is of any typetemplate<classT1>classship<int,t1>//<T1,int> can, the position of int and T1 can be exchanged{     Public: Ship (T1 A1,intA2): a (A1), B (A2) {}voidShow () {cout<<a<<" "<<b<<Endl; }    Private: T1 A; intb;};intMain () { ship<Double,int> S (1.222,2);//call a generic version or call a generic version to make the compiler choose the most specific version when life is partially special, and when no partial specificity is available, select the generic versionship<Double,Double> s2 (6.66,9.99) ;//invoking a specific version ofs.show ();    S2.show (); return 0;}

Attention:

The definition of partial specificity does not conflict with the definition of a generic template, which can have a completely different set of members than a generic template, and the generic definition of a class template member never uses a special member that instantiates a template part

Overloading and Function templates

(1) Create a candidate set for the function:

A. Any common function with the same name as the back tone function

B. Arbitrary template instantiation, template arguments Discover template arguments that match the function parameters used in the call

(2) Determine which common function's behavior is feasible, and each instance template in the candidate set is feasible

(3) If a conversion is required to make a call, arrange the feasible function according to the kind of conversion, the template function instance conversion is limited

A. If there is only one function to choose from, call him

B. There are multiple optional, ambiguity, remove all template instances

(4) rearranging the available functions that remove instances of a function template

A. There is a call

B. Otherwise it has ambiguity

    1. If only one function is optional, call this function
    2. Otherwise it has ambiguity
//function Templatestemplate<TypeName TintcomConstT&,Constt&);//Common FunctionsintcomConst Char*,Const Char*);Chars1[]="Sdfa", s2[]="DFASKF"; COM (s1,s2);//The normal function is called because the function converts the array to a pointer, calling the normal function takes precedence over the calling template
Transform and Reload function templates

The non-template version is limited when the normal function matches the template function equally and as well

function modules, class templates

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.