C + + function template Two (function template overloading)

Source: Internet
Author: User

//function Template Overloading#include <iostream>using namespacestd;/*function templates are strictly type-matched, and template types do not provide implicit type conversions ordinary functions are capable of automatic type conversions*//* function template overloading four rules 1 function templates can be overloaded like normal functions 2 C + + compiler takes precedence over normal function 3 If the function template can produce a better match, then select template 4 can pass the syntax of the empty template argument list to qualify the compiler to match only through templates */Template<typename t>voidTest1 (t A, T b) {cout<<"executed the template function Test1 ()"<<Endl;}voidTest2 (intAintb) {cout<<"executed the Test2 ()"<<Endl;}voidTest1 (intAintb) {cout<<"performed a common function Test1 ()"<<Endl;}voidTEST3 (intAintb) {cout<<"performed a common function Test3 ()"<<Endl;}voidProtecta () {intA =1; intb =2; Charc ='3'; //Test1 (A, c); Error error C2782: "Void Test1 (T,t)": template parameter "T" ambiguous    /*because A is an int, c is a char type and the function template requires that both parameters are T-type, which means that the type of the two arguments must be the same, but the int and char types are inconsistent so the error*/Test2 (A, c);//for normal functions, implicit type conversions are possible, so no problem}voidPROTECTB () {intA=1, b=2; Charc ='3', d='4'; Test1 (A, b);//call the normal function hereTest1<> (A, b);//call the template function here--rule 4Test1 (c, D);//call the template function here--rule 3---Assume that no template function will still call the normal functionTest3 (c, D);}voidMain () {PROTECTB (); System ("Pause");}

C + + function template Two (function template overloading)

Related Article

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.