Deep Exploration of generic programming (i) Summary of advanced features of templates

Source: Internet
Author: User
Tags class definition expression function prototype inheritance

Some basic template features:

Non-class parametric template

The parameters declared by the template may not be class parameters, and the non class arguments that can be declared include integers (double,float cannot), enums, object references, or pointers.

To implement a class or non-class parameter load overload on a class method parameter through a stencil nesting (implementation at call time, not implemented when defined)

Friend function Template:

Direct examples:

Template <typename t>

Friend ostream& operator<< <T> (ostream& os, const t& T) {};

Inline template functions:

The template definition must be in the header file, so the defined function is not inline, and you need to be sure to declare that the function is inline.

inline void Func () {}; In the template class definition

inline void A<t>:: func{}; Outside the template class definition

Advanced template Features:

Stencil nesting

When a parameter of a template class is invoked, whether it is a class parameter or a non class parameter, another template parameter is required, which is nested with the stencil.

It is noteworthy that the template nesting cannot be handle when the parameter template parameters and the original template parameters are the same class, therefore, you must keep the original case of no template nesting to complete the template method function overload.

In addition, the template nesting priority is:

For a method that does not use the parameter template, that is, the parameters are all using the original template parameters, individually defined, individually invoked.

Once a nested template is used in the parameter, a nested template method must be triggered, in the order that the

Triggers a method overload that only changes all the template parameters, and if not, the escalation includes more nested templates that do not change parameters.

The test code is as follows:

Template <typename t,int score>
class vec{
    T A;
    int SC;
Public:
    T geta () const {return A;}
    int Getscore () const {return SC;}
    VEC (t): A (t), SC (SCORE) {};
    Template nested copy constructs are not used in parameters, must have
    VEC (const vec<t,score>& VV): A (VV.A), SC (Vv.sc) {cout<< "Copyt is called" <<endl;};
    Parameter score Template copy Construction
    template <int other>
    vec (const vec<t,other>& VV): A (Vv.geta ()), SC ( Vv.getscore ()) {cout<< "Copyother is called" <<endl;};
    Both parameters T and score have occurred in the template copy construction
    template <typename e,int other> vec
    (const vec<e,other>& VV): A ( Vv.geta ()), SC (Vv.getscore ()) {cout<< "copy is called" <<endl;};
    Virtual ~vec ();
};

Template partial specificity and function template overload bias

Template by rewriting a specific class to implement the template of the partial specificity (that is, a specific class is not in accordance with the template implementation), the function does not feature a biased, therefore, by overloading implementation.

The priorities are:

Determines the class, determines the pointer/reference/Object template class, and the sequential invocation of any template class.

Special, template-specific

All template parameters are special, at this point, functions can be special, and the function overrides of the special class need not be added to the template declaration template<>

Partial-specificity test code

#include <iostream>
    
template <typename t>
class pointertemp{public
:
    T name;
    Pointertemp (): Name (T ()) {std::cout<< "Original Ctr callled" <<std::endl;};
};
    
Template <typename t>
class pointertemp<t*>{public
:
    t* name;
    Pointertemp (): Name (nullptr) {std::cout<< "t* Ctr callled" <<std::endl;};
};
    
Template <>
class pointertemp<char*>{public
:
    char* name;
    Pointertemp (): Name (nullptr) {std::cout<< "char* Ctr callled" <<std::endl;};
};
    
Template <typename k>
void Showk (k name) {
    std::cout<< "K func called";
}
    
Template <typename k>
void Showk (k* name) {
    std::cout<< "k* func called";
}
    
void Showk (char* name) {
    std::cout<< "char* func called";
}

It is important to note that when T is a pointer or reference, the compiler will take precedence over the unspecified template when it cannot find a specific implementation of a matching concrete class that has a special reference to the pointer.

And when the pointer is special and the reference is special, it is instantiated with <A*>, but the template parameter is actually a, that is, <A*> instantiation actually assigns a to T and then uses the pointer-specific template.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Template inheritance:

Template classes can implement inheritance and polymorphism, general, subclasses inherit the same parameter instance class of the parent class, if you want to inherit an instance class of any class parameter, just declare a parent class parameter in the template more than once.

Template parameter Templates

The template is nested to template the method parameters of the class template, which is used to overload the implementation of a special case type of a template or overload a class set;

Often used to declare a template, his argument is a template class instance

The basic syntax is as follows, when the template parameter template is used, template parameters are instantiated in the template definition, and no external implementation is required.

The class used to instantiate the template parameter can be any other class parameter that determines the class or template declaration.

The meaning of the template parameter template is that when the template is defined, the parameters of its parametric template are determined, and it is not necessary to be determined by the caller at the time of the call, which distinguishes the following three template statements:

The template parameter template allows any class to be used for parametric templates of the template, but the class is instantiated in the definition of the original template.
template <typename T, Template<typename e> class container>
class temptemp{
    //template parameters are instantiated here
};
Call: Temptemp<int,vector>
    
    
//template parametric templates use the OK class, which requires the user to declare it, but only to declare the class, and once the other class is declared, the compiler will make an error
template <typename T , TypeName container<t>>
class A;
Call: Temptemp<int,vector<int>>
//Call: Temptemp<int,vector<double>> is a compilation error
    
Template <typename T, typename container<e>>
class A;
Call: Temptemp<int,vector<int>> is a compile error
//call: Temptemp<int,vector<double>>
    
// Both of these can implement the template overload

The basic usage is as follows:

Template <typename T, Template<typename E, TypeName allocator=allocator<e>> class container>
Class temptemp{public
:
    container<t> vt;
    Container<int> Vint;
    void Show () {
        vt.resize);
        Vint.resize (ten);
        vt.at (0) =3;
        vint.at (0) =12;
        std::cout<<vt[0]<< "\ T" <<vint[0]<<endl;
    }
};

Type derivation Template:

Distinguish between auto and Decltype

Both return a type name, and the return value usage does not differ from int or custom A.

The difference is:

Decltype (expr) dynamically determines a type at the time of running this sentence, and the interior needs a certain expression (cannot contain a stencil).

Auto is a placeholder, indicating that the type to be determined here is determined after the variable initialization or function declaration.

Variables are determined directly by initialization, and the function return value is determined by trailing, that is, adding-> decltype (expr) after the function declaration (at this point, the template object, because the template is compiled to the moment when the type has been determined) or the determined variable and expression.

The auto parameter is not allowed to appear in the function prototype!

Examine such a type of derivation of the template:

Template <typename t,typename e>
auto func (const t& a,const e& B)->decltype (a*b) {
    Auto S=DECLT Ype (A*b) ();
    return s;
}

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.