c++ - Why is std::hash not specialised for std::reference_wrapper? -
i thought have been, can't find in standard library implementation (gcc-4.8.2).
why std::hash
not specialised std::reference_wrapper
?
#pragma once #include <functional> namespace std { template<typename t> struct hash<reference_wrapper<t>> { size_t operator()(const reference_wrapper<t>& r) const { return std::hash<t>()(r.get()); } }; }
std::reference_wrapper
used provide reference semantics in utilities default copying values, such std::bind
.
direct use of std::reference_wrapper
in container pointer (except not nullable). hashing of pointers (and smart pointers) follows reference (i.e. address) semantics.
you can provide own hash function, of course. if define template on pointers , smart pointers, t*
might better choice of value type rather reference_wrapper<t>
.
note, if you're hashing objects , storing hashes, might eliminate duplicates keeping in unordered_map
. value identity , object identity same.
Comments
Post a Comment