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

531 views Asked by At

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
3

There are 3 answers

0
Geoff Romer On BEST ANSWER

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.

4
R Sahu On

You have to define the specialization before you can use it.

namespace std
{
   template <>
   struct std::hash<void (foo::*)(int)>
   {
       // Implement the class.
   };
}

Use it.

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

change the struct declaration to:

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

Non-static member functions have a hidden parameter that corresponds to the this pointer, that's why the compiler happens.