C ++ template parameters

Source: Internet
Author: User

C ++ programming language is a powerful computer application language. Its appearance has greatly reduced the burden on developers and increased development efficiency. Here we will first take a look at the concepts related to the C ++ template parameters. Simply put, templates can be considered as a type, and function templates are no exception.

Since it is a type, we should use an instance of the template function. Since it is the relationship between types and instances, there should be a type instantiation problem. When instantiating a common type, we usually need to provide the necessary parameters, and the template function is no exception. Only C ++ template parameters are not common parameters, but specific types. That is to say, when instantiating a function template, the type must be used as the parameter. Generally, template parameters are classified into template parameters and call parameters. For example:

 
 
  1. Template <typename T1, typename T2, typename RT>
  2. Inline RT const & max (T1 const & a, T2 const & B)
  3. {
  4. // TODO: code implementation
  5. ........
  6. }

The first line defines the function template parameters, and the second line defines the call parameters. Note that the return value does not belong to the call parameters of the function template.

When calling a template, the most important thing is that the C ++ template parameters can be correctly exported during the call. Note the following points:

1: display the instantiated function template. For example:

 
 
  1. Template <typename T>
  2. Inline T const & max (T const & a, T const & B)
  3. {
  4. Return a <B? B:;
  5. }
  6. // Instantiate and call a template
  7. Max <double> (4, 4.2 );

In row 10, a template is instantiated by specifying the C ++ template parameter as double.

2: implicitly instantiate a function template. For example:

 
 
  1. Template <typename T>
  2. Inline T const & max (T const & a, T const & B)
  3. {
  4. Return a <B? B:;
  5. }
  6. // Implicitly instantiate and call a function Template
  7. Int I = max (42, 66 );

Row 3: The specified function template parameter is not displayed, but it can automatically push and export the function template parameter as int. There may be a problem. What will happen if the definition of a non-template function is the same as that of the derived template function instance? For example:

 
 
  1. Inline int const & max (int const & a, int const & B)
  2. {
  3. // For easy differentiation, the returned result is + 100
  4. Return a <B? A + 10: B + 100;
  5. }
  6. Template <typename T>
  7. Inline T const & max (T const & a, T const & B)
  8. {
  9. Return a <B? B:;
  10. }
  11. // Is the called template function or non-template function?
  12. Int I = max (42, 66 );

In fact, the code in the second line first goes back to check whether there are non-template functions that meet the requirements. If not, match and instantiate the corresponding template functions based on the parameters. Therefore, it calls a non-template max function.

3: You can also use some default C ++ template parameters.You do not need to specify all template parameters. For example, you can specify some parameters from left to right. For example:

 
 
  1. // Three parameters are defined from left to right.
  2. Template <typename RT, typename T1, typename T2>
  3. Inline RT const & max (T1 const & a, T2 const & B)
  4. {
  5. // TODO: code implementation
  6. ..
  7. }
  8. // You can specify only the first response parameter. That is, the double type must be returned.
  9. Max <double> (4, 4.2 );

In the code above, because the returned parameter type does not belong to the call parameter, you must specify it as double. T1 and T2 are parameters that call the C ++ template and can be derived from the function call.

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.