C ++ learning note 36: template specialization and template instantiation)

Source: Internet
Author: User

C ++ learning note 36: template specialization and template instantiation)

C ++ templates may not be able to process certain types.

For example:

# Include
 
  
Using namespace std; class man {private: string name; int data; public: man (string s, int I): name (s), data (I) {} void show () const {cout <"this name is" <
  
   
Compilation result:
   


You can see many errors. (Although this is not the focus .) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/Signature + signature/M5buv0tS8sNL + signature + 6zc/Signature + hozwvcD4KPHA + PGJyPgo8L3A + signature/Signature + Signature = "brush: java; "> template <> void mSwap (man & m1, man & m2)

Or

template<>void mSwap
 
  (man &m1,man &m2)
 

EXAMPLE After modification:

# Include
 
  
Using namespace std; class man {private: string name; int data; public: man (string s, int I): name (s), data (I) {} void show () const {cout <"this name is" <
  
   
Void mSwap (man & m1, man & m2) {int temp = m1.getData (); m1.setData (m2.getData (); m2.setData (temp ); cout <"Here are the man version, successed! "<
   
    
Run:
    


This is the role of an explicit template.

It should be noted that the normalization takes precedence over the general template, while the non-template function takes precedence over the embodiment and the general template.


2. instantiation

To learn more about templates, you must understand the instantiation and embodiment of terms. Remember, the function template itself does not generate function definitions in the Code. It is just a solution for generating function definitions. When the compiler uses a template to generate a function definition for a specific type, the template instance is obtained. For example, the above mSwap (I, j), the function call mSwap (I, j) this causes the compiler to generate an instance of mSwap (), which uses the int type.

A template is not a function definition, but an int template instance is a function definition. This instantiation method is called implicit instantiation.

C ++ can also be explicitly instantiated.

Syntax: declare the required category-use the <> symbol to indicate the type, and add the keyword template before the declaration:

Templata void mSwap (Man &, man &)

This statement indicates "using the mSwap () template to generate man-type function definitions ".




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.