0

I know similar questions have been asked on this topic before, but I could not find an answer that applied to my case particularly.

I’m using Visual 2015, and I noticed when I got a C206 compilation error in xhash (“term does not evaluate to a function take 2 args”) for passing two different hash functions to the unordered_map constructor that the stl unordered_map constructor does not have an override that takes two hash classes/functions. You can only apply the hash to either the key or the value but not both. Does that mean I will have to override the pair class and add another constructor that takes two Hash functions (How would I go about doing this efficiently)?

Is it bad practice in general to have an object as a key and value? For instance, in my case I was trying to map a pair to an Edge object and vice versa. From my understanding if both objects are hashable, there should be no reason that a size_t value can’t map to another size_t value. Why then, isn’t there a default implementation for that constructor.

I also recognize that I could make an intermediate map that takes the objects and maps them to their hash values or just hash both objects first and map the size_t values but that seems cumbersome. Is there a better way than the ones I’ve mentioned above?

7ujh6
  • 1
  • If not, you need to provide [mcve] of what you have tried and how it didn't work. – Yksisarvinen Jan 01 '20 at 18:17
  • Re: "You can only apply the hash to either the key or the value but not both" -- you can only apply the hash to the key. That's what the key is for: each key is associated with a value. The question has been closed as a duplicate of an answer about how to manage keys; it seems to perhaps be about creating a two-way lookup, which is a different matter. If that's what you want, clarify the question, including code. – Pete Becker Jan 01 '20 at 18:29
  • Probably just a misconception about hashing. _Only_ the key is hashed not the value. Then this hash is used to quickly find the value that is associated with the key. There is no need to hash the value. Another interpretation of the question might be, that you got a `std::pair` as key and tried to pass a hash-function for `X` and one for `Y` to the constructor, where you really would need a hash function for `std::pair`? – Lukas-T Jan 01 '20 at 18:47
  • Thanks, that does clarify things, and sorry for the redundancy. I did in fact think that you had to hash the key and the value in an unordered_map. – 7ujh6 Jan 01 '20 at 18:55

0 Answers0