Can std::hash be used to hash function pointers?
Asked Answered
L

3

20

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!

Laze answered 1/11, 2013 at 2:54 Comment(7)
Is this simply theoretical? I don't think the default hash function for function pointers has any practical use, since (in most implementations) it simply hashes the integer underlying the pointer.Itu
@C.R.- It's partially theoretical, though I'm also curious if you could make something like a std::unordered_map<void(*)(), std::string> that could be an inverse map from strings to functions or something of that ilk.Laze
I did some tests here and I saw that function pointers successfully cast to void*.Danielldaniella
@MuriloVasconcelos for your particular implementation, yes. But the standard gives no guarantees.Oink
Although it's strictly not legal, I daresay that it must "work" because function pointers to the same function are guaranteed to compare equal, and function pointers to a different function must compare not equal. This somehow implies that every function is a different "thingie", no matter what exactly it is. Thus, if the hash is worth its salt, it should hash to a unique value.Porta
@C.R., why is that not practically useful? It means every function will hash to a distinct value, giving a perfect hash.Archivolt
@JonathanWakely: I was wrong. I thought good hashing must map related inputs to seemingly random outputs, and forgot the ultimate purpose is to avoid collisions.Itu
O
12

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.

Oink answered 1/11, 2013 at 3:14 Comment(2)
Smart pointers are definitely covered by the standard ([util.smartptr.hash] 20.7.2.6). Still looking to see if code pointers are.Fluctuation
Nothing in the standard says hash<T*> doesn't apply to any pointer type, so it does apply. Specifically, it applies to any "pointers to void or objects or functions (including static members of classes) of a given type" (the different types of pointer given in [basic.compound])Archivolt
S
1

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.

Satellite answered 8/11, 2013 at 4:17 Comment(5)
The question does not involve any non-type template parameters. T is a type parameter.Donothing
@AndreyT Mhhh... I either am misreading or misunderstanding the standard... care to explain?Satellite
It is pretty straightforward. There are three kinds of template parameters: #1) type parameters, e.g. template <typename T> ..., #2) non-type parameters, e.g. template <int N> ..., #3) template parameters, e.g. template <template <typename U> class C> .... In this case we are dealing with an ordinary type parameter (#1). Meanwhile, you provided a quote that applies specifically to non-type parameters (#2). So, I just don't see how your quote is relevant here.Donothing
Thanks. I will modify accordingly.Satellite
This paragraph is not about (some made-up) Hashable, but about Hash. It defines what kinds of types can be used to hash things, not which kinds of things are hashable. The OP asks if some specialization of std::hash (which meets the requirements of Hash) can be used to hash a function pointer; not if function pointers meet the Hash requirements.Gath
F
-1

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++.

Feast answered 22/4, 2015 at 12:39 Comment(1)
That has nothing to do with it. A member function pointer is not a function pointer in the slightest.Indium

© 2022 - 2024 — McMap. All rights reserved.