In-depth exploration of generic programming, Summary of advanced features of templates

Source: Internet
Author: User

 

Some basic template features:

Non-class parameter templates

The parameters declared by the template can be non-class parameters. The non-class parameters that can be declared include integers (double, float is not allowed), Enum, object references, or pointers.

Using template nesting to overload class or non-class parameters to carry class method parameters (implemented when called, not implemented when defined)

 

Youyuan function template:

Example:

Template <typename T>

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

 

Inline template functions:

The template definition must be included in the header file. Therefore, the defined function is not inline. You must make sure that the declaration function is inline.

Inline void func () {}; // In Template Class Definition

Inline void a <t >:: func {}; // outside the Template Class Definition

 

Advanced template features:

Template nesting

 

When a parameter is called for a template class, whether it is a class parameter or a non-class parameter, another template parameter is required and nested with the template.

Note that template nesting cannot be handle. When the parameters of the parameter template and the parameters of the original template are of the same class, you must reload the template method functions in the original condition without template nesting.

 

In addition, the priority of template Nesting is:

If parameter templates are not used, that is, all parameters are defined and called separately by using the original template parameters.

Once a nested template is used in the parameter, a nested template method is triggered. The trigger sequence is,

Trigger only changes method overloading of all template parameters. If not, trigger more nested templates without changing parameters step by step.

 

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) {}; // The copy structure of the nested template is not used in the parameter, and VEC (const VEC <t, score> & vv): A (vv. a), SC (vv. SC) {cout <"copyt is called" <Endl ;}; // The copy structure template of the parameter score template <int other> VEC (const VEC <t, other> & vv): A (vv. geta (), SC (vv. getscore () {cout <"copyother is called" <Endl ;}; // templates are generated for both parameter T and score. <Typename E, int other> VEC (const VEC <E, other> & vv): A (vv. geta (), SC (vv. getscore () {cout <"copy is called" <Endl ;}; virtual ~ VEC ();};

 

 

Bitwise templates and function templates

 

By Rewriting a specific class, a template is biased (that is, a specific class is not implemented according to the template). functions are not biased. Therefore, they are implemented through overloading.

Priority:

Determine the class, determine the template class of the pointer/reference/object, and call any template class in sequence.

 

Special: All Special templates

All template parameters are made special. At this time, functions can be made special, and the template declaration template is not required for function rewriting of special classes. <>

 

Biased 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 worth noting that when T is a pointer or reference, when the special implementation of the matching specific class cannot be found, the compiler will find whether the pointer is special or the reference is special, these give priority to non-specific templates.

 

<A *> instantiation is used when pointers are made and references are made, but the template parameter is actually a, that is, <A *> instantiation actually gives a to T, then use the pointer-specific template.

 

Template inheritance:

The template class can be inherited and polymorphism. Generally, the subclass inherits the same parameter instance class of the parent class. If you want to inherit the instance class of any class parameter, you only need to declare one more parent class parameter in the template.

 

Template parameter Template

Template Nesting is used to template the method parameters of the class template, and is specially used to overload the implementation of a special case type of the template or to overload a class set;

 

It is often used to declare a template. Its parameter is a template class instance.

The basic syntax is as follows. When a template parameter template is used, the template parameters of the template are instantiated within the template definition without external implementation.

The class used to instantiate template parameters can be any class parameter determined by class or template declaration.

 

The template parameter template determines the parameters of its parameter template when defining the template, and does not need to be determined by the caller during the call. The declaration of the following three templates should be distinguished:

// The template parameter template allows the template parameter template to use any class, but the class is instantiated and determined in the original template definition. Template <typename T, template <typename E> class container> class temptemp {// The template parameter is instantiated here}; // call: temptemp <int, vector> // The template parameter template uses a definite class. The class must be declared by the user, but only the definite class can be declared. Once other classes are declared, the compiler reports the error template <typename t, typename container <t> Class A; // call: temptemp <int, vector <int> // call: temptemp <int, vector <double> is the incorrect template <typename T, typename container <E> Class A; // call: temptemp <int, vector <int> is a compilation error // call: temptemp <int, vector <double> // the preceding two templates can be overloaded.

 

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(10);        vint.resize(10);        vt.at(0)=3;        vint.at(0)=12;        std::cout<<vt[0]<<"\t"<<vint[0]<<endl;    }};

 

Type derivation template:

 

Differences Between Auto and decltype

 

Both return a type name, and its return value is no different from Int or custom.

 

The difference between the two is:

When running this sentence, decltype (expr) dynamically determines a type and requires a definite internal expression (the template cannot be included ).

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

Variables are determined directly through initialization, And the return value of the function is determined by trailing, that is, after the function declaration is added-> decltype (expr) (in this case, the template object can be used, because the template has determined the type at the moment of compilation) or determined variables and expressions.

 

Auto parameters cannot appear in the function prototype!

 

Evaluate the knowledge of such a type derivation template:

template <typename T,typename E>auto func (const T& a,const E& b) ->decltype(a*b){    auto s=decltype(a*b)();    return s;}

 

# To be continued

 

In-depth exploration of generic programming, Summary of advanced features of templates

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.