I have a class (call it Outer
) which has a private member class (Inner
). I want to store instances of Outer::Inner
in unordered standard containers, so I want to specialize std::hash<Outer::Inner>
.
However, when writing this:
namespace std {
template<>
struct hash<Outer::Inner> {
std::size_t operator()(const Outer::Inner &arg) const
{
return std::hash<int>()(arg.someSpecialProperty);
}
};
}
the compiler complains:
error: 'Inner' is a private member of 'Outer'
std::size_t operator()(const Outer::Inner &p) const
^
I have tried to make std::hash
a friend struct by following this answer, but that didn't work either: the forward declaration of Outer::Inner
failed with:
error: use of undeclared identifier 'Outer'
So how should I proceed (if what I intend to do is possible at all)?
std::hash
template, right? – Hertelfriend struct std::hash<Inner>;
insideOuter
? (Probably after declaring or definingInner
.) And then define your specialisation after. – Miksen