I have been under the impression that casting objects to void* and back to the same type is OK. However, casting void* to pointer-to-member-function doesn't even compile (with GCC).
According to this understanding, something like the following should be OK (the member function pointer is not cast anywhere):
Code:
#include <iostream>
class Fred
{
public:
int f(char x, float y) { std::cout << x << ' ' << y << '\n'; }
};
// FredMemFn points to a member of Fred that takes (char,float)
typedef int (Fred::*FredMemFn)(char x, float y);
#define CALL_MEMBER_FN(object,ptrToMember) ((object)->*(ptrToMember))
struct FredAndMethod
{
Fred* fred;
FredMemFn fun;
float y;
char x;
};
void fred_caller(void* data)
{
FredAndMethod* fm = static_cast<FredAndMethod*>(data);
CALL_MEMBER_FN(fm->fred, fm->fun)(fm->x, fm->y);
}
void callit(void (*f)(void*), void* data)
{
f(data);
}
int main()
{
Fred o;
FredAndMethod fm = { &o, &Fred::f, 3.14f, 'x' };
callit(fred_caller, &fm); // okay
}
Of course, I might be wrong, but even Stroustrup in his technical FAQ shows casting void* to int*.