C ++ template function reloads different comments

Source: Internet
Author: User

In C ++ programming language, a special function is called a template function. But in fact, it is somewhat the same as a common function. Here we will explain some precautions for C ++ template function overloading, which are different from those of common function applications.

1: If the instantiated template function is called the same as a non-template function, the non-template function is called. However, you can also specify to call the template function, such:

 
 
  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. // The form of the template function after instantiation is the same as that of a non-template function.
    You can use the following method to call the Template Function
  12. Int I = max <> (42, 66 );

2: Since the parameters reloaded by the c ++ template function are of type, type conversion is not supported. But non-template functions support type conversion:

 
 
  1. Inline int const & max (int const & a, int const & B)
  2. {
  3. Return a <B? B:;
  4. }
  5. Template <typename T>
  6. Inline T const & max (T const & a, T const & B)
  7. {
  8. Return a <B? B:;
  9. }
  10. // Because the template function does not support type conversion, the non-template max function will be called here
  11. Max ('C', 42.2 );

In the above Code, if max ('C', 42.2) wants to call the max function of the template, it must meet the conditions that both parameters and return values are of the same type. The given two parameters have different types, and the template function does not support type conversion. Therefore, if it cannot find the matching template function, it will call the non-template max function. However, if we forcibly use max <> ('C', 42.2) to call the template function, a compilation error will occur.

The preceding section describes the function overloading of the C ++ template.

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.