I want to overload the std::hash template for my custom type called AnimationSet:
struct AnimationSet {
const AnimationData *animationData;
SceneNode *sceneNode;
bool operator==(const AnimationSet &other) const {
return ( this->animationData == other.animationData &&
this->sceneNode == other.sceneNode );
}
};
As you can see, it's a struct that contains just two pointers.
Is it legit to cast those pointers to unsigned int in order to calculate the hash for AnimationSet?
namespace std {
template<>
struct hash<AnimationSet> {
size_t operator()(const AnimationSet &set) const {
hash<unsigned int> h;
return h((unsigned int)set.animationData) ^ h((unsigned int)set.sceneNode);
}
};
}
Edit: I'm asking this question in the context of hash overloading, but I'd like to know the answer to the more general question: "Is it fair to cast any pointer to unsigned int?"