I assume that there is a reason behind this design choice. Boost seems to have an implementation for it, hence it should be possible to use vectors as hash table keys. Are there any theoretical properties for hash functions applied to arrays that make them more prone to collisions or other undesirable behavior?
1 Answer
You'll notice Boost doesn't actually have an extension to accept a vector<T> as a key specifically - instead it has an extension that lets you use any Iterable - and it generates the hash only as a function of the Iterable's contents...
This may or may not be desirable, depending on:
- If you want to use object-identity rather than object-value as the basis for hashing... or not.
- If you're comfortable with hashing being a non-constant-time operation... or not.
- Just because
boost::hash_rangeappears to beO(n)doesn't mean the underlying iterable won't take 5 minutes to return all hashable values for each call...
- Just because
- If the order of elements does - or doesn't matter.
- (I believe) using
boost::hash_rangeorboost::hash_combinewith one of two distinct but equivalentunordered_setobjects will result in different hash-codes despite their value-equivalence.
- (I believe) using
- If two conceptually different objects that can iterate over the same values (e.g. a
vector<uint8_t>representing a data buffer, orqueue<SomeEnum>whereSomeEnum : uint8_trepresenting a queue of values) should have the same hahs-code... or not.
I suspect the team behind the STL doesn't like the fact that there's so many contextual "if"s described above which would mean it wouldn't be sensible to provide default behaviour and so they require you to always be more explicit with your hash-generation for arbitrary objects (besides, if you want Boost's behaviour, then just use Boost in the first place - it's not like Boost is competing with the STL).
Also see this QA: C++ unordered_map using a custom class type as the key
std::hashforstd::vector, making sure that the user knows what they're getting into if they want to use one as a key. That's a guess, though.