Search code examples
c++hashstdhash

Unable to Use Specialization of std::hash for Pointer-to-Member?


I assume that std::hash's specialization for generic pointer types can be used for pointer-to-members, however I am unable to use it as such; instead, my compiler gives me an "incomplete type" error, which I assume means that is' not using the specialization of std::hash for pointers? What's going on here?

#include <functional>

struct foo{
    void bar(int a){}
};

int main(){
    std::hash<void (foo::*)(int)> hasher;
}

The error:

..\src\m.cpp:43:32: error: aggregate 'std::hash<void (foo::*)(int)> hasher' has incomplete type and cannot be defined

Solution

  • The trouble is that pointers to member are not really pointers; they just happen to have a similar name and kind of similar syntax. std::hash is specialized for ordinary pointers, but not for pointers to members. You could of course specialize it yourself, but if there's a way to do that that's guaranteed to be safe, I'm not seeing it; there's not much you can do with a pointer to member other than dereference it or cast it to other pointers to members.