Coupon Accepted Successfully!


Overloading Of Template Functions

A template function also supports the overloading mechanism. It can be overloaded by a normal function or a template function. While invoking these functions, an error occurs if no accurate match is met. No implicit conversion is carried out in the parameters of template functions. The compiler observes the following rules for choosing an appropriate function when the program contains overloaded functions:
  1. Searches for an accurate match of functions; if found, it is invoked
  2. Searches for a template function through which a function that can be invoked with an accurate match can be generated; if found, it is invoked
  3. Attempts a normal overloading declaration for the function
  4. In case no match is found, an error will be reported
17.10 Write a program to overload a template function.
template <class A>
void show(A c)
cout<<“\n Template variable c=”<<c;
void show (int f)
cout<<“\n Integer variable f=”<<f;
int main()
return 0;
Template variable c = C
Integer variable f = 50
Template variable c = 50.25
Explanation: In the above program, the function show() is overloaded. One version contains template arguments, and the other version contains integer variables. In main(), the show() function is invoked thrice with char, int, and float values that are passed. The first call executes the template version of the function show(), the second call executes the integer version of the function show(), and the third call again invokes the template version of the function show(). Thus, in the manner described above, template functions are overloaded.

Test Your Skills Now!
Take a Quiz now
Reviewer Name