I am trying to implement HashTable in C++ via templates. Here is the signature:
template<class T1, class T2>
class HashTable {
public:
void add(T1 a, T2 b);
void hashFunction(T1 key, T2 value)
{
// how to implement this function using key as a generic
// we need to know the object type of key
}
};
So, I am unable to move ahead with implementation involving a generic key.
In Java, I could have easily cast the key to string and then be happy with implementing the hash for a key as string. But, in C++, what I know is that there is a concept of RTTI which can dynamically cast an object to the desired object.
How to implement that dynamic cast, if this method is correct at all?
If using template is not the correct approach to implement generics for this case, then please suggest some better approach.
T1has to be "hashable". I.e. a method that gives you the hash key must exist.std::unordered_setin C++11? It is a hash table in the Standard Library.