What is the difference between function templates and function overloading?
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
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.