C++ Comparing Member Function Pointers
In C++, is it possible to define a sort order for pointers to member functions? It seems that the operator< is undefined. Also, it's illegal to cast to void*.
class A
{
public:
void Test1(){}
void Test2(){}
};
int main()
{
void (A::* const one)() = &A::Test1;
void (A::* const two)() = &A::Test2;
bool equal = one == two; //Equality开发者_Python百科 works fine.
bool less = one < two; //Less than doesn't.
return 0;
}
Thanks!
Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member function (in which case the result is unspecified).
Of course, you can always introduce an ordering by implementing a comparison predicate and comparing the pointers explicitly (won't look too elegant though, since you can only use equality comparisons). Other possible solutions would cross into the territory of the various implementation-specific "hacks".
Member function pointers are not actual pointers. You should look at them as opaque structs. What does a method pointer contain:
struct method_pointer {
bool method_is_virtual;
union {
unsigned vtable_offset; // for a virtual function, need the vtable entry
void* function_pointer; // otherwise need the pointer to the concrete method
}
};
If you could cast this to void* (you can't) all you would have is a pointer the the struct, not a pointer to code. That's why operator<() is undefined as well since the value of the struct's pointer is just where ever it happens to be in memory.
In addition to that, what are you sorting by?
精彩评论