In books on C++, why are we taught to use function overloading when we have templates at our disposal in C++?
Wouldn’t it be better to show the effective (and correct) use of templates? As opposed to where function overloading is taught in most books on C++?
Or, are there good reasons to use one instead of the other?
Templates provide an advantage when you want to perform the same action on types that can be different. A simple example:
You can use overloading when you want to apply different operations depending on the type:
You could achieve the above using templates and template specializations, but such specializations should represent a few exceptions to the general case.