What is the difference between function templates and function overloading?

Questions by faasi113

Showing Answers 1 - 9 of 9 Answers

efranford

  • Aug 11th, 2008
 


Function templates involve telling a function that it will be receiving a specified data type and then it will work with that at compile time.

The difference with this and function overloading is that function overloading can define multiple behaviors of function with the same name and multiple/various inputs.

eg:
Function Template

template<Class T>
void print(T var)
{
 cout << var;
}

The template T is basically passing an instance of a specified "Class" T into the function to work with.  With this you can really only use one Template and other inputs.  It is possible to overload a functions template.

Functoin Overloading
string ToString()
{
  return this;
}

string ToString(string)
{
  return this + string;
}

Notice how the second example is overloaded to allow the same name to be used while attaining a different response.

  Was this answer useful?  Yes

sureshds

  • Dec 26th, 2009
 

In both cases function name is same.

But, during function overloading, the function signature is different. Signature is like type of arguments, number of arguments and orderof arguments.

For templates the signature is same and ready to accept any kind of data. There is no restriction on type of data.

  Was this answer useful?  Yes

Function templates makes the function to performs the same task for different data type.

Function overloading makes the function to performs different task for different parameters you are passing.
 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions