Templates only work if all of the code for the different types would be exactly the same in form, just with the different types substituted in.
Granted, overloading and templates aren’t mutually exclusive. Here is a sample set of functions I used for a desktop C++ program that can add variables of different types into an array of bytes for writing into a file. The first function adds a single object to the end of the binary array, the second adds an array of elements, and the 3rd adds the elements of an STL container to the byte array.
Don’t worry so much about the exact details of what functions I’m using here, just take it as an example of the way templates and overloading can be combined for maximum flexibility.
void append_raw(binary_t &v, const T &data, ENDIAN big_endian)
for( size_t i=0; i<sizeof(T); i++ )
v.push_back((char)((data>>(8*((bool)big_endian?(sizeof(T)-i-1):(i)))) & 0xFF));
void append_raw(binary_t &v, const T* data, size_t n, ENDIAN big_endian)
for( size_t i=0; i<n; i++ )
append_raw(v, data[i], big_endian);
void append_raw(binary_t &v, const Container &c, ENDIAN big_endian)
for( typename Container::value_type i : c )
append_raw( v, i, big_endian );