Overload template Functions

Source: Internet
Author: User
# Include <iostream>
Using Namespace STD;
// Returns the maximum value of two int values.
Inline Int Const & MAX ( Int Const &, Int Const & B)
{
Return A <B? B:;
}
// Evaluate the iterator of two values of any type
Template <typename T>
Inline t Const & MAX (T Const & A, T Const & B)
{
Return A <B? B:;
}
Int Main ()
{
: Max ( 7.0 , 42.0 );// Call max <double> (using real arguments)
: Max ( ' A ' , ' B ' ); // Call max <char> (using real arguments)
: Max ( 7 , 42 ); // Call an int-heavy non-Template Function
: Max <> ( 7 , 42 ); // Call max <int> (using real arguments)
: Max < Double > ( 7 , 42 ); // Call max <double> (using real arguments)
: Max ( ' A ' , 42.7 ); // Call an int-heavy non-Template Function
Return 0 ;
}

A non-template function can coexist with a function template with the same name, and the function template can also be instantiated as this non-template function. For non-template functions and function templates with the same name, if other conditions are the same, during the call, the overload parsing process usually calls non-template functions.

Max <> () tells the compiler that only the template can match the call, and all the template parameters should be deduced according to the call real parameters.

Max ('A', 42.7) calls int-heavy non-template functions: the template does not allow automatic type conversion, but normal functions can perform automatic type conversion.

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.