Others have indicated that you cannot do this cast (strictly speaking, when you click void* everything that is used with reinterpret_cast is also forbidden, but tacitly carried by compilers. static_cast is for use here).
I usually do the following that does type pun, and this is the recommended way to do it, according to the dlopen man page (which is to do the reverse translation from void* to the function pointer). Taking the address of the function pointer, you get a pointer to the data: a pointer to a function pointer. This will allow you to pass it to void* . It pretends to point to void* (instead of a function pointer), and then reads it.
Test* (*pF)(void **a); void *p = *(void**)(void*)&pF;
An intermediate agent added to void* makes it equivalent to be used internally in static_cast internally, and makes GCC silent about the warning of a pun. Using a C ++ Style cast, it looks like a combination of two static_cast 's
void *p = *static_cast<void**>(static_cast<void*>(&pF));
I found that using this method, GCC automatically notices when the left and right types are different in size, and spit out a warning in this case. Needless to say, as with all methods that try to circumvent this limitation, this behavior is undefined.
If you have a function and want void* point to it, you can do it all in one line, but it's a little cumbersome in syntax. Here's how to do it, but I do not recommend it if you have problems reading it - you can use it inside a macro
The trick is to start being able to make a type, but it must convert the pointer of the temporary function to a lvalue link on const, to which you can take the address, and then continue as described above.
Using explicit C ++ styles static_cast , this looks a lot more complicated because you have to consider it. C style styles automatically touched this. Enjoy!
int main() { Test** pF(void **a); void *p = *static_cast<void* const*>( static_cast<void const*>( &static_cast<Test** (* const&)(void **a)>(&pF))); }