C + + template explicit instantiation, explicit materialization, implicit instantiation of __c++

Source: Internet
Author: User
Tags class definition function definition
Transfer from: Helloword's Blog
Link: http://blog.sina.com.cn/s/blog_62b1508e0100hl8a.html

A function template is a new type of C + + that allows you to define only one implementation of a function, and you can call the function with different types of arguments. Doing so reduces the complexity of the code's writing and is also easy to modify (note: Using a template function does not reduce the size of the final executable, because when the template function is invoked, the compiler is instantiated based on the parameter type at the time of the call). Here's how to use the function template:

struct job {char name[20];
int salary;

};

Template <class t>//function template declares that the common variable type is t void Swap (T &a, T &b);
void Showjob (const job &a)//print job content using Std::cin;
Using Std::cout;
Using Std::endl;
    void Main (void) {int a = 4;
    int b = 5;
    cout<< "before swap a =" <<a<< "b=" <<b<<endl;
    Swap (A, b);
   
    cout<< "after swap a =" <<a<< "b=" <<b<<endl;
    Job Joba = {"Coder", 10000};  
    Job Jobb = {"Manager", 1000};
    cout<< "before swap";
    Showjob (Joba);
    Showjob (Jobb);
    cout<<endl;
    Swap (Joba, jobb);
    cout<< "after swap";
    Showjob (Joba);
    Showjob (Jobb);
cout<<endl;
System ("pause");

};
    Template<class t>//function template implements void Swap (T &a, t &b) {T temp;
    temp = A;
    A = b;
b = temp; } void Showjob (const job &a) {cout<< "" <<a.name<< "=" <<a.salary;} 


What if you only want to swap salary in the above job structure swaps without changing the value of other member variables? There are several ways to solve this problem in C + +.

1> explicit materialization of
Explicit materialization is also based on a function template, except that a specific type of materialized function is added to the function template based on a different implementation.

Template<>void swap<job> (Job &a, job &b)
{
     int salary;
     salary = A.salary;
     A.salary = b.salary;
     B.salary = salary;
}



As shown above, the implementation of the materialized function is inconsistent with the template, and the compiler resolves the function call by selecting the most matching function definition.

2> defines a general function with the same name

void swap (Job &a, job &b)
{
     int salary;
     salary = A.salary;
     A.salary = b.salary;
     B.salary = salary;
}


Because the compiler selects the most matching function definition when overloading parsing, the compiler chooses the void swap (Job &a, job &b) function definition and masks the template function when calling swap (Joba, Jobb).

Also, template functions can be overloaded, and their operations are consistent with regular functions.

Template <class t> void swap (t &a, T &b);

Template <class t> void swap (t &a, T &b, T &c);


Template <typename t> void swap (t &a, T &b)
{
    T temp;
    temp = A;
    A = b;
    b = temp;
}

Template <typename t> void swap (t &a, T &b, T &c)
{
    T temp;
    temp = A;
    A = b;
    b = C;
    c = temp;
}


The above is mainly about the embodiment of the function template, the following is the template instantiation.

function Template:

#define MAXNAME 128
struct job
{
char name[maxname]:
int salary;

Template<class t>
void swap (t &a, T &b)
{
  T temp;
  temp = A;
  A = b;
  b = temp;
};

template void swap<int> (int &a, int & B);  Explicitly instantiated, simply declare

template<> void swap<job> (Job &a, Job &b)   //Explicit materialization (as already mentioned above, note that is separate from the instantiation area, Must have a definition)
{
  int salary:
  salary = a.salary:
  a.salary = b.salary;
  B.salary = salary;
};/ /explicite Specialization.


Class Template:

Template <class t>
class arrary
{
private:
  t* ar;
  int l;
...
};/ /template class declaration.

Template class array<int>;   Explicit instantiation. Explicit instantiation of

template<> class array<job>
{
private:
  job* ar;
  int l;
};/ /expicit Specialization.   Explicit materialization, the class definition body can be different from the class template array



Accordingly, implicit instantiation means that the compiler does not generate the declaration and definition instances of the template until the template is used. The compiler generates an instance of the corresponding type based on the template definition only when the template is used. Such as:
int i=0, j=1;
Swap (I, j); The compiler implicitly generates a function definition for the swap<int> (int &a, int &b) based on the type of the parameter i,j.
The array<int> arval;//compiler implicitly generates array<int> class declarations and class function definitions based on the type parameters.

An explicit instantiation:
When the template is explicitly instantiated, the compiler generates the template instance based on the specified type explicitly instantiated before the template is used. As shown earlier, instantiate (explicit instantiation) template functions and template classes. The format is:
Template TypeName function<typename> (argulist);
Template class classname<typename>;
Explicit instantiation requires only declaration and does not need to be redefined. The compiler implements instance declarations and instance definitions based on the template.

Display materialization:
For some special types, the implementation of the template may not be appropriate, and the implementation needs to be redefined, at which point the display materialization (explicite specialization) can be used. The display instantiation needs to be redefined. The format is:
Template<> TypeName Function<typename> (argu_list) {...};
Template<> class classname<typename>{...};
Fully: template<> void swap<job> (Job &a, Job &b) {...}; is the explicit materialization of a function template, meaning that the job type does not apply to the definition of the function template swap, and therefore is redefined by this explicit materialization; can also be simplified writing template<> void swap (Job &a, job &b); template void swap<job> (Job &a, Job &b); is an explicit instantiation of a function template that simply declares that the compiler encounters this explicit instantiation and generates an instance function directly based on the definition of the original template and the declaration. This function accepts only the job type. Otherwise, the compiler encounters the use of the template to implicitly generate the corresponding instance function.

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.