Hi,
I cannot say if your solution is portable or not. It would seem to be basically the same thing that I do with asFUNCTION() and asMETHOD(), but it is possible that in converting the method pointer to void* you risk loosing some vital information, if the compiler lets you do that conversion at all.
The pointer size is not just variable between compiler to compiler, it can even be variable within the same compiler. Visual C++ uses 5 different sizes for method pointers, ranging from 4 to 20 bytes, depending on the type of the class and wether the compilation is for a 32bit processor or a 64bit processor.
For 32bit processors:
- normal class method: 4 bytes, the pointer points to the function implementation
- virtual class method: 4 bytes, the pointer points to a function stub that reads the virtual function table and jumps to the true function implementation
- virtual class method with multiple inheritance: 8 bytes, a function pointer, and an offset to the true virtual function table
- virtual class method with virtual inheritance: 12 bytes, a function pointer, and an offset to the true virtual function table, plus another offset to find the true object. One needed value is missing from this pointer type, since the MSVC++ compiler hardcodes it when the method is invoked
- class method for an unknown implementation: 16 bytes, the same as the virtual inheritance pointer, except that the missing value is now included.
On 64bit processors, an additional 4 bytes are needed, since all pointers are 64bit.
On GNUC the method pointer is always 8 bytes in size independently of the class type (I would think it is 12 bytes on 64bit processors but cannot say for sure). And they manage to keep all the important information in those 8 bytes. They use a trick where the least significant bit of the function pointer shows wether it is an address or an offset in the vftable.
If you are interested in more details, there is a really interesting article on the code project that explains everything you need:
Member Function Pointers and the Fastest Possible C++ DelegatesRegards,
Andreas