C ++ templates Note 3 overload function template (additional)

Source: Internet
Author: User

# Include <iostream> <br/> # include <cstring> <br/> # include <string> </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:; <br/>}</P> <p> // maximum of two pointers <br/> template <typename T> <br/> inline T * const & MAX (T * const &, T * const & B) <br/>{< br/> return * A <* B? B:; <br/>}</P> <p> // maximum of two C-strings <br/> inline char const * const & MAX (char const * const &, <br/> char const * const & B) <br/>{< br/> return STD: strcmp (a, B) <0? B: A; <br/>}</P> <p> int main () <br/>{< br/> int A = 7; <br/> int B = 42; <br/>: max (a, B); // max () for two values of Type int </P> <p> STD: String S = "hey"; <br/> STD: String T = "you "; <br/>: max (S, T); // max () for two values of Type STD :: string </P> <p> int * P1 = & B; <br/> int * P2 = & A; <br/>: max (P1, P2 ); // max () for two pointers </P> <p> char const * S1 = "David"; <br/> char const * S2 = "Nico "; <br/>: max (S1, S2 ); // Max () for two C-strings <br/>}</P> <p>/* <br/> in all the above overloaded implementations, we all pass each real parameter through reference. When reloading the function template, it is best to change only the content that needs to be changed! <Br/> therefore, inline char const * const & MAX (char const * const & A, char const * const & B) <br/> */</P> <p> # include <iostream> <br/> # include <cstring> <br/> # include <string> </P> <p> // maximum of two values of any type (call-by-Reference) <br/> template <typename T> <br/> inline t const & MAX (T const & A, T const & B) <br/>{< br/> return a <B? B: A; <br/>}</P> <p> // maximum of two C-strings (call-by-value) <br/> inline char const * max (char const * a, char const * B) <br/> {<br/> return STD: strcmp (A, B) <0? B: A; <br/>}</P> <p> // maximum of three values of any type (call-by-Reference) <br/> template <typename T> <br/> inline t const & MAX (T const & A, T const & B, T const & C) <br/> {<br/> return max (a, B), c); // error, if Max (A, B) uses call-by-value <br/>}</P> <p> int main () <br/>{< br/>: max (7, 42, 68); // OK </P> <p> const char * S1 = "Frederic"; <br/> const char * S2 = "Anica "; <br/> const char * S3 = "Lucas"; <br/>: max (S1, S2, S3 ); // error </P> <p >}< br/>/* <br/> note that calling return max (a, B), c) is incorrect! For C-string, max (a, B) generates a new temporary local value. This value may be returned by the Max function outside of China as a reference, this will cause invalid references to be returned !! <Br/> the possibility of this error is only possible, not inevitable! Inline t const & MAX (T const & A, T const & B, T const & C) returns a reference! <Br/> */</P> <p> template <typename T> <br/> inline t const & MAX (T const & A, T const & B) <br/>{< br/> return a <B? B:; <br/>}</P> <p> // 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 ); // uses the template version even for ints <br/>}// because the following declaration comes <br/> // too late: <br/> // 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>/* <br/> the declaration of all overloaded versions of the function should be prior to the position where the function is called <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.