I'm unable to google out the answer to this question.
When should I use templates vs template overload vs plain function overload ?
I have a preference to always use templates, it feels like less typing and more generic code, but started to question my self, why do people even use function overload when we have templates.
The only difference I see is that in case of templates the default template declaration will handle all cases which specialized cases won't, while with plain functions we would have to write at least twice as much code.
Also in 3rd example below sometimes a single template is enough. So the main question here is why bother with plain function overload?
For example :
void func(int param)
{
// ...
}
void func(double param)
{
// ...
}
void func(char param)
{
// ...
}
The same can be achieved with templates :
template<typename T>
void tmpl(T param)
{
// ...
}
template<>
void tmpl(double param)
{
// ...
}
template<>
void tmpl(char param)
{
// ...
}
In most cases we don't even need to overload templates because all we care about is to make sure data type size is correct while algorithm of the function is the same.
So in these cases we can just write this:
template<typename T>
void tmpl(T param)
{
// ...
}
while with plain function we would need to do much more work.
EDIT: I found this good answer which is somewhat relevant but does not cover entirely my question: