如何哈希和比较指针到成员函数? [英] How to hash and compare a pointer-to-member-function?

查看:146
本文介绍了如何哈希和比较指针到成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何哈希(std :: tr1 :: hash或boost :: hash)一个c ++指针到成员函数?

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function?

示例:

我有几个bool(Class :: * functionPointer)()(不是静态)指向类的几个不同的方法,我需要哈希那些指针 - 成员函数。

I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function.

我如何做呢?

)那些成员函数指针,所以我可以将它们存储在std :: set?

Also how can i compare (std::less) those member function pointers so i can store them in a std::set?

推荐答案

所有C ++对象,函数,在内存中表示为字符数组。所以你可以尝试:

All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try:

bool (Class::*fn_ptr)() = &Class::whatever;
const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&fn_ptr));

现在将 ptrptr 视为指向数组(sizeof(bool(Class :: *)()))字节,并且散列或比较这些字节。如果您愿意,可以使用 unsigned char 而不是 char

Now treat ptrptr as pointing to an array of (sizeof(bool (Class::*)())) bytes, and hash or compare those bytes. You can use unsigned char instead of char if you prefer.

这保证没有误报 - 在C ++ 03中,指向成员函数的指针是POD,这意味着它们可以使用memcpy进行复制。这意味着如果具有相同的字节对字节值,则它们是相同的。

This guarantees no false positives - in C++03, pointers to member functions are POD, which means among other things that they can be copied using memcpy. This implies that if have the same byte-for-byte values, then they are the same.

问题是成员函数指针的存储表示可以包括位不参与值 - 因此它们不一定对于同一成员函数的不同指针是相同的。或者编译器可能,由于一些晦涩的原因,有不止一种方法指向同一个类的相同的函数,它们不是字节方式相等。无论哪种方式,你可以得到假阴性。你必须看看成员函数指针如何在你的实现上工作。它必须实现 operator == 成员函数指针,如果你可以找到如何,你可以找出一个顺序和哈希函数。

The problem is that the storage representation of member function pointers could include bits which do not participate in the value - so they will not necessarily be the same for different pointers to the same member function. Or the compiler might, for some obscure reason, have more than one way of pointing to the same function of the same class, which are not byte-wise equal. Either way you can get false negatives. You'll have to look into how member function pointers actually work on your implementation. It must implement operator== for member function pointers somehow, and if you can find out how then you can probably figure out an order and a hash function.

这可能很难:成员函数指针是尴尬的,根据指向哪种类型的函数,存储可能包括不同数量的非参与空闲空间(虚拟,继承)。因此,您可能必须与编译器的实现细节进行非常明显的交互。本文可能有助于您开始: http://www.codeproject.com/KB/cpp /FastDelegate.aspx

That's potentially hard: member function pointers are awkward, and the storage is likely to include different amounts of non-participating "slack space" according to what kind of function is pointed to (virtual, inherited). So you'll probably have to interact quite significantly with your compiler's implementation details. This article might help get you started: http://www.codeproject.com/KB/cpp/FastDelegate.aspx

一个更清洁的替代方法是通过数组进行线性搜索,以便规范化所有的函数指针,然后比较和基于数组中该函数指针的规范实例的位置的哈希。取决于您的性能要求。即使有要求,类(和它的派生类)有这么多的函数,线性搜索将需要这么长时间?

A cleaner alternative might be to do a linear search through an array in order to "canonicalise" all your function pointers, then compare and hash based on the position of the "canonical" instance of that function pointer in your array. Depends what your performance requirements are. And even if there are requirements, does the class (and its derived classes) have so many functions that the linear search will take that long?

typedef bool (Class::*func)();
vector<func> canon;

size_t getIndexOf(func fn_ptr) {
    vector<func>::iterator it = find(canon.begin(), canon.end(), fn_ptr);
    if (it != canon.end()) return it - canon.begin();
    canon.push_back(func);
    return canon.size() - 1;
}

这篇关于如何哈希和比较指针到成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆