struct AnimationSet { const AnimationData *animationData; SceneNode *sceneNode; bool operator==(const AnimationSet &other) const { return ( this->animationData == other.animationData && this->sceneNode == other.sceneNode ); }};
如您所见,它是一个只包含两个指针的结构.
将这些指针强制转换为unsigned int以计算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); } };}
编辑:
我在哈希重载的上下文中问这个问题,但我想知道更普遍的问题的答案:“将任何指针转换为unsigned int是否公平?”
你应该#include< cstdint>并转而使用std :: uintptr_t.这是一个无符号整数类型,保证能够存储指针的值而不会丢失任何位.
总结以上是内存溢出为你收集整理的c – 将指针视为unsigned int for hash全部内容,希望文章能够帮你解决c – 将指针视为unsigned int for hash所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)