I have a template class Bazthat contains a nested class Sub. I would like to define a hash function for this subclass, specializing in std :: hash. However, this does not seem to work.
#include <functional>
struct Foo {
struct Sub {
};
};
template <class T>
struct Bar {
};
template <class T>
struct Baz {
struct Sub {
int x;
};
};
namespace std {
template <>
struct hash< Foo::Sub >;
}
namespace std {
template <class T>
struct hash< Bar<T> >;
}
namespace std {
template <class T>
struct hash< Baz<T>::Sub >;
}
namespace std {
template <class T>
struct hash< typename Baz<T>::Sub >;
}
Gcc 4.5.3 complains:
$ g++ -std=c++0x -c hash.cpp
hash.cpp:34:30: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::hash’
hash.cpp:34:30: error: expected a type, got ‘Baz<T>::Sub’
hash.cpp:40:12: error: template parameters not used in partial specialization:
hash.cpp:40:12: error: ‘T’
UPDATE
What I'm really trying to do is implement a container that maintains stable references (not in the sense of C ++) to elements inside it. I want to allow the user to embed these links in std::unordered_setand similar, and use them to effectively access or modify existing elements. Below is the layout, not the specific container that I am implementing. The problem is defining a hash function for the reference type.
template <class T>
class Container {
public:
class Reference {
public:
private:
size_t index;
};
Reference insert (const T &value);
Reference find (const T &value);
void remove (Reference r);
Reference first ();
Reference next (Reference prev);
private:
struct Entry { T value, ... };
std::vector<Entry> m_entries;
};