Display materialization, display instantiation, implicit instantiation

Source: Internet
Author: User
Tags function definition

To discuss the differences between the three, we need to know the function template first, the function template is a new addition to C + +, and he creates a generic function to support a variety of different types of parameters, avoiding the repetitive design of the function body, when we need to reuse a piece of code but need to handle different types of arguments, Using a function template is a convenient means his biggest feature is the data type also as a parameter. Each function uses a function template to allow several functions to use a common template parameter, and you should use a template class

Display instantiation:

Template  void  swap<int> (int, int);

Show Materialization: (define job as a struct)

Template <> void swap<job> (Job &,job &);

       or     template<>  void Swap   (Job &,job &);

We can see from the definition form that the difference is that the former begins with template and the latter begins with template<>.
In terms of meaning, display materialization tells the compiler not to use the function template of swap () to generate a function definition, while using a specialized job type to explicitly define a function definition is to provide a materialized function definition that includes the required code, and when the compiler finds the materialized definition that matches the function call, The definition is used instead of looking for a template, and display instantiation generates an instance function that indicates a particular parameter type

To sum up:
Implicit instantiation : Before using a template, the compiler does not generate the declaration and definition examples of the template, and the compiler will generate an instance function based on the template.
explicit instantiation : Is the compiler generates an instance function whether or not there is a program
display materialization : Because for some special types, it may not be suitable for the template implementation, you need to redefine the implementation, this time using the scene that shows materialization

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.