Suppose I need to make some kind of template container Map on a hash table. The container, naturally, can take on a key the value of any type - how, in that case, to write a hash function? How to cast an arbitrary type T (for which the cast operator may not be overloaded) to an integer type? It seems like a reinterpret_cast should help, which leads to a pointer, but the construction of the form
reinterpret_cast <size_t*> (key); Fails to compile when key is a non-integer type.
If we bring the pointer to the pointer (as in the example on msdn)
reinterpret_cast <size_t*> (&key); then the hash of the table is lost - a search through it will not work, since a key with an identical value will have a different address, and the result of the hash function will also be different.
Is it possible to somehow forcibly interpret the bytes of an arbitrary object in memory as an integral type? Generally, which solution will be the right one? How is this problem solved in std::unordered_map or QMap ?
*(int *)(&key)but that’s the point ... - pavelstd::unordered_mapthis problem is not solved. try for example to createstd::unordered_map< pair<int,int> , int >- pavel