C ++ templates Note 2 overload function Template

Source: Internet
Author: User

// Maximum of two int values <br/> inline int const & MAX (INT const & A, int const & B) <br/>{< br/> return a <B? B:; <br/>}</P> <p> // maximum of two values of any type <br/> template <typename T> <br/> inline t const & MAX (t const &, t const & B) <br/>{< br/> return a <B? B: A; <br/>}< br/>/* <br/> if other conditions are the same for non-template functions and function templates with the same name, during the call, the reload parsing process <br/> usually calls non-template functions, instead of generating an instance from the template. <Br/> */<br/> // maximum of three values of any type <br/> template <typename T> <br/> inline t const & MAX (T const &, t const & B, T const & C) <br/>{< br/> return: max (a, B), c ); <br/>}</P> <p> int main () <br/> {<br/>: max (7, 42, 68 ); // callthe template for three arguments <br/>: max (7.0, 42.0); // callmax <double> (by argument deduction) <br/> :: max ('A', 'B'); // callmax <char> (by argument deduction) <br/>: max (7, 42 ); // callthe nontemplate for two ints <br/>: max <> (7, 42); // callmax <int> (by argument deduction) <br/> // explicitly specify an empty list of real parameters for the template. This syntax tells the compiler that only the template can match the call. <br/> :: max <double> (7, 42); // callmax <double> (no argument deduction) <br/>: max ('A', 42.7 ); // callthe nontemplate for two ints <br/> // The template does not allow automatic type conversion, but normal functions can perform automatic type conversion, therefore, a non-template function will be called <br/>}< br/>

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.