You do not need to add the template
word at all. You just overload the template function.
class hashtable{
public:
template <typename T>
void insert(T item){
/* Do Stuff */
};
void insert(string item){
/* Do different stuff.
Implementation can go to *.cpp file if necessary.
*/
};
};
Beware, though, regardless of the design you can get in trouble when you try to insert
the types which are implicitly convertible to std::string, including const std::string &
and const char*
. Believe it or not, in C++03 std::string had even implicit constructor from int
!
That is why people usually prefer to templetize the hashtable class, not the function.