views:

227

answers:

2

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 works fine.
    bool less = one < two; //Less than doesn't.

    return 0;
}

Thanks!

+3  A: 

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

AndreyT
So it's impossible and has no workaround?
Imbue
The work around is as I suggested, which is to implement the member function pointer as a functor.
jmucchiello
A: 

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?

jmucchiello
While it is true that the general physical structure of a member pointer is different from the structure of an ordinary (non-member) pointer, the reason for that has absolutely noting to do with any "self" pointers. In C++ pointers to members are not attached to concrete "selves" in any way. What you describe above looks like a "closure" implementation, but C++ has no closures.
AndreyT
I've rewritten my answer. I don't know what I was thinking.
jmucchiello
jmucchiello, A method pointer does not contain any pointer to an instantiated object of any sort. A method pointer is a pointer to code, not an object/struct or whatever else you want to call it.
Imbue