0

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
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Shokwav
  • 654
  • 8
  • 19

3 Answers3

2

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.

Geoff Romer
  • 2,358
  • 1
  • 18
  • 19
1

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.

Matt
  • 6,010
  • 25
  • 36
0

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;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • That "Implement the class" glosses over the hard part of the problem- it's far from clear how to compute a hash value for a pointer-to-member. – Geoff Romer Nov 26 '14 at 22:30
  • @GeoffRomer, I agree. I am not sure the OP knows how to implement the class either. I was helping to explain the compiler error and how to overcome that. – R Sahu Nov 26 '14 at 22:34
  • @R Sahu as far as I know, there is no well-defined way to take the hash of a pointer-to-member. I was just (incorrectly) assuming that the pointer specialization of std::hash covered them. – Shokwav Nov 27 '14 at 01:02
  • @Shokwav, Perhaps the answer to this SO post might help. http://stackoverflow.com/questions/1328238/how-to-hash-and-compare-a-pointer-to-member-function – R Sahu Nov 27 '14 at 01:57