Just to summarize all the questions you made in the question and comments. Basically, the answer to this question is probably the following:
The right choice to use is reinterpret_cast 1 & dagger; & Dagger; .
This is a lot of footnotes! I guess this makes it a real C ++ question. In any case, I would not ask about this in an interview - this is a trick question too much - and there are many real questions with real, direct answers that you can ask instead.
1 On a 64-bit system, you cannot any_kind_of_cast a void* until uint32_t . So they are all wrong.
& dagger; There is the problem of finding a name in C ++ 11 between the hash() provided by the user and the class std::hash<T> . You should obviously associate them with why using namespace std; considered bad practice?
& Dagger; Even if there were no problems above that prevented the compilation of the code in the first place, is the goal is to actually hash the pointers to a rather than the values ββof a ? The most likely answer should be:
auto val = *static_cast<const uint32_t*>(p);
If it comes to hashing pointers, then reinterpret_cast will be correct, but not uint32_t (according to (1)). We would like to use uintptr_t :
auto val = reinterpret_cast<uintptr_t>(p);
In the case of 64-bit code, you probably want to do something than val ^ (val >> 16) , since you are actually ignoring the upper 32-bit ones, but at least this will lead you to the correct execution, which compiles everywhere .
Barry source share