Basic concepts of C ++ function templates

Source: Internet
Author: User

The template application in C ++ programming language is a very important application for program development. The C ++ function template can process different types of data with the same program code. The key is to describe the processed data type as a parameter, that is, type parameterization.

The general form of defining a function template is:

 
 
  1. Template <class type parameter name 1, class type parameter name 2,...>
  2. Function return value type function name (table of parameters)
  3. {
  4. Function body
  5. }

Note:

(1) Here, the type parameter name is a common parameter name that represents all types of data. It can represent the basic data type or the class.

(2) the method for compiling the C ++ function template is:

Step 1: define a common function. The data type uses a specific common data type. For example, the print array is used as an example. First, define a common function that prints an integer array:

 
 
  1. void printArray(int *array,int count)  
  2. {  
  3. for (int i=0;i<count;i++)  
  4. cout<<array[i]<<" ";  
  5. cout <<endl;  

Step 2: parameterize data types: replace all specific data type names (such as int) with abstract type parameter names (such as T) defined by yourself ).

Step 3: Use the keyword template to declare the type parameter name before the function header. In this way, a specific function is transformed into a general function template:

 
 
  1. template <class T>void printArray(T *array,int count)  
  2. {  
  3. for (int i=0;i<count;i++)  
  4. cout<<array[i]<<" ";  

  • Interpretation of the C ++ TinyXml Application Method
  • Basic Content of C ++ Singleton Mode
  • Analysis of related methods for creating Web services in C ++
  • How to initialize the C ++ Constructor
  • Skills related to C ++ identifier naming rules
(3) The C ++ function template is not an executable function. It only describes the function program and does not generate Execution Code for the compiled program.

(4) When the Compilation Program encounters a function call:

When the function name (real parameter table) is used, the specific data type in the real parameter table is automatically replaced with the type parameter in the function template to generate an implicit overload function, the program code of this overload function is the same as the function template, and the type uses the specific data type in the real parameter table. For example, when the program displays the function call statement printArray (a, aCount, the compiler will replace all T in the function template with the specific data type int Based on the Data Type int of the first real parameter, this produces a specific definition of the executable function (this process is called template instantiation ):

 
 
  1. void printArray(int *array,int count); 

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.