Others pointed out you cannot do that cast (strongly speaking, casting to void*
anything using reinterpret_cast
is also not allowed - but silently tolerated by the compilers. static_cast
is intended to be used here).
I usually do the following, which is doing a type pun, and is the recommended way to do it, according to the manpage of dlopen
(which is about doing the converse - casting from void*
to a function pointer). Taking the address of the function pointer will give you a data-pointer: Pointer to a function pointer. This will allow you to cast it to void*
. It pretends it's pointing to a void*
(instead of a function pointer), and then reads it.
Test* (*pF)(void **a);
void *p = *(void**)(void*)&pF;
The intermediary cast to void*
makes it equivalent to using two static_cast
's internally, and makes GCC be quiet about warning about a type pun. Using C++ Style casts, this looks like a combination of two static_cast
's
void *p = *static_cast<void**>(static_cast<void*>(&pF));
I found that using this technique, GCC automatically notices when the left and the right types differ in size, and spit out a warning in that case. Needless to say that like with all techniques that try to work around this limitation, this is undefined behavior.
If you have a function, and want a void*
pointing to it, you can do it all in one line, but that's a bit cumbersome on the syntax. Here it is how this can be done, but i don't recommend it if you have problems to read it - you may however use it inside a macro
// using the function declaration you provided
Test** pF(void **a);
void *p = *(void**)(void *) &(Test** (* const&)(void **a))&pF;
The trick to start being able to do the type but is to transform the temporary function pointer to an lvalue reference to const, which you can take the address of, and then proceed like above.
Using explicit C++ style static_cast
casts, this looks much more complicated, because you have to take the constness into account. The C style cast automatically dealed with that. Have fun!
int main() {
Test** pF(void **a);
void *p = *static_cast<void* const*>(
static_cast<void const*>(
&static_cast<Test** (* const&)(void **a)>(&pF)));
}