Can std::hash be used to hash function pointers?

2019-02-11 20:43发布

Can the C++11 std::hash type be used to hash function pointers? There is a hash partial specialization defined as

template <typename T> struct hash<T*>;

but since function pointers are different from other pointer types in C++ (e.g. they can't be cast to void*), I'm not sure whether it is safe to use it for types like int(*)() or void(*)(int, int).

Is this permitted? Is there any specific wording in the new ISO spec that supports or refutes this?

Thanks!

3条回答
劫难
2楼-- · 2019-02-11 21:04

It's actually interesting... I bumped into this question while using MSVC++. What I'm trying to do is:

static std::unordered_map<Fun, std::string> FunctionMap()
{
    static std::unordered_map<Fun, std::string> map;
    return map;
}

with Fun a function pointer type.

During compilation, I get the following error:

error C2338: The C++ Standard doesn't provide a hash for this type.
....
_Kty=int (__thiscall Testje::* )(int,int)

In a previous attempt I attempted to cast the function pointer to void*, which isn't allowed and doesn't compile (see: https://isocpp.org/wiki/faq/pointers-to-members#cant-cvt-memfnptr-to-voidptr for details). The reason is that a void* is a data pointer, while a function pointer is a code pointer.

My conclusion so far is that it isn't allowed and it won't compile on MSVC++.

查看更多
虎瘦雄心在
3楼-- · 2019-02-11 21:09

Great question. I don't know the answer for sure, and I'm happy to defer to anyone with better knowledge than me, but my thinking is that even though function pointers aren't the same as data pointers, they are pointers nonetheless: so the std::hash<T*> partial specialisation should be applied.

For what it's worth, the following compiles without warnings even with -pendantic in g++ 4.8.1 and clang 3.3, and works as expected:

#include <functional>
#include <iostream>

void func1(int) {}
void func2(int) {}

int main()
{
    typedef void (*func_type) (int);

    std::hash<func_type> hash;

    std::cout << hash(func1) << std::endl;
    std::cout << hash(func2) << std::endl;

}

I'd be really interested if anyone has any references to the standard to back this up though.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-11 21:20

I found the following:

17.6.3.4 Hash requirements

A type H meets the Hash requirements if:

  • it is a function object type (20.8)

[...]

And then, the referenced 20.8 states:

A function object type is an object type (3.9) that can be the type of the postfix-expression in a function call (5.2.2, 13.3.1.1).228 A function object is an object of a function object type. In the places where one would expect to pass a pointer to a function to an algorithmic template (Clause 25), the interface is specified to accept a function object. This not only makes algorithmic templates work with pointers to functions, but also enables them to work with arbitrary function objects.

It kind of states it backwards... but the statement not only makes algorithmic templates work with pointers to functions... seems appropriate for your question.

查看更多
登录 后发表回答