When the function template encounters a function overload

Source: Internet
Author: User

Demo 1

#include <iostream>using namespace std;//let type parameterization = = =, easy for programmers to encode//generic programming//template tell the C + + compiler I'm going to start generic programming. See T, don't give the wrong error tem Plate <typename t>void Myswap (t &a, t &b) {T c = 0;c = A;a = B;b = c;cout << "Hello ...." I am a template function welcome calll me "<< Endl;} void Myswap (int a, char c) {cout << "a:" << a << "C:" << C << endl;cout << "I am a normal function Welcome to visit "<< Endl;} void Main () {inta = 10;charc = ' z '; Myswap (A, c);//Normal function invocation:  can be implicitly type-cast Myswap (c, a);//Call function template Myswap (A, a);//Function Templates function Invocation (Essence: Type parameterization): Will strictly match by type, does not perform automatic type conversion cout<< "Hello ..." <<endl;system ("pause");
With demo 1 You can get:

function template and common function difference conclusion:

1, function template does not allow automatic type conversion;

2, the ordinary function can be automatic type conversion;


Demo 2

/* Function template does not allow automatic type conversion ordinary functions can be automatic type conversion */#include "iostream" using namespace Std;int max (int a, int b) {cout<< "int max (int A, int b) "<<endl;return a > B?" A:B;} Template<typename t>t Max (t A, T B) {cout<< "T Max (t A, T b)" <<endl;return a > B? a:b;} Template<typename t>t Max (t A, T B, t C) {cout<< "T Max (t A, T B, t C)" <<endl;return Max (Max (A, B), c);} void Main () {int a = 1;int B = 2;cout<<max (A, B) <<endl;//When the function template and normal function conform to the call, the normal function cout<<max<> (a , b) <<endl; If the function template is displayed, use the <> type list Cout<<max (3.0, 4.0) <<endl; If a function template produces a better match using a function template Cout<<max (5.0, 6.0, 7.0) <<endl; Heavy Duty Cout<<max (' a ', +) <<endl;  Calling a normal function can implicitly type convert system ("pause"); return;}
Through the analysis of demo 2, we can get:

The calling rules for function templates and common functions:

1, function template can be overloaded like normal functions;

2, C + + compiler takes precedence of common functions;

3, if the function template can produce a better match, then choose the template;

4, the syntax of the empty template argument list can be limited to the compiler only through template matching;

Remember these 4 points and you can avoid a lot of mistakes.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

When the function template encounters a function overload

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.