I thought it would have been, but I can't find this in my standard library implementation (gcc-4.8.2).
Why is std::hash
not already specialised for 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());
}
};
}
reference_wrapper
on? It's only member isget()
which returns aT&
(and the function call operator which does the same). It exists almost solely to allow references to be stored inside standard containers. – Norkstd::addressof(r.get())
. – Futchreference_wrapper
. I still need to provide a specialisation for the wrapped typeT
. so I have to provide 2 specialisations. If I want to hash on the pointer, I can specialise hash for my typeT
and do that. If I want to hash on something else, I still need to specialise hash for my typeT
(or provide another hash object). I still don't get what the problem would be havingstd::reference_wrapper
pass through toT
as standard. After all,hash
is specialised for other wrapper types likeunique_ptr
et al – Nork