Sign up ×

I want to reinterpret cast a function pointer into a void* variable. The type of the function pointer will be of type Class* (*)(void*).

Below is the sample code,

class Test
    int a;

int main()
    Test* *p(void **a);
    void *f=reinterpret_cast<void*>(p);   	

The above code works well with Visual Studio/x86 compilers. But with ARM compiler, it gives compilation error. Don't know why.

Error: #694: reinterpret_cast cannot cast away const or other type qualifiers

I read the explanation in

I was concerned about the below explanation.

Casting between function pointers and regular pointers (e.g. casting a void (*)(void) to a void*). Function pointers aren't necessarily the same size as regular pointers, since on some architectures they might contain extra contextual information. This will probably work ok on x86, but remember that it's undefined behavior.

How to do such conversions from void (*)(void*) -> void* effectively so that atleast it compiles almost the same in most of the compilers ?

share|improve this question
You should at least put in an assert that sizeof (void*) >= sizeof (Test**(*)(void**)). If this assert fails, then obviously there's no way to store the function pointer in a void*. If the assert does not fail, it still doesn't guarantee anything, but at least it's possible that the cast could work. –  Steve Jessop Aug 20 '09 at 9:49
Note that p actually has type Test**(void**), and is a function, not a function pointer. Putting a space in "Test* *" does't affect parsing. –  MSalters Aug 20 '09 at 10:11

5 Answers 5

up vote 5 down vote accepted

reinterpret_cast can't be used to cast a pointer to function to a void*. While there are a few additional things that a C cast can do which aren't allowed by combination of static, reinterpret and const casts, that conversion is not one of them.

In C the cast is allowed, but it's behavior isn't defined (i.e. even round trip isn't guaranteed to work).

Some POSIX functions need the conversion to be well useful.

I've played with several compilers I've here:

  • none prevent the C cast, even in the highest conformance mode. Some give a warning depending on the warning and conformance level, others gave no warning whatever I tried.
  • the reinterpret_cast was a error with some compilers even in the more relaxed level while other accepted it in all case without ever giving a warning.

In the last available draft for C++0X, the reinterpret_cast between function pointers and objects pointers is conditionally supported.

Note that if that make sense or not will depend on the target more than the compiler: a portable compiler like gcc will have a behavior imposed by the target architecture and possibly ABI.

As other have make the remark,

Test* *p(void **a);

defines a function, not a pointer to function. But the function to pointer to function implicit conversion is made for the argument to reinterpret_cast, so what reinterpret_cast get is a Test** (*p)(void** a).

Thanks to Richard which makes me revisit the issue more in depth (for the record, I was mistaken in thinking that the pointer to function to pointer to object was one case where the C cast allowed something not authorized by C++ casts combinations).

share|improve this answer
C cast worked perfectly. Thanks. :) –  vprajan Aug 20 '09 at 9:33
(Nearly -1) I don't think this is right, can you provide a reference for this? It's my understanding that C-style cast is defined in terms of the new cast styles. Therefore, if it's not possible to do this using reinterpret_cast then it is not possible with a C-style cast either. –  Richard Corden Aug 20 '09 at 9:50

reinterpret_cast can only be used to

  • add constness
  • convert a pointer to an integral type large enough to hold it and back
  • convert a pointer to a function to a pointer to a function of different type
  • convert a pointer to an object to a pointer to an object of different type
  • convert a pointer to a member function to a pointer to a member function of different type
  • convert a pointer to a member object to a pointer to a member object of different type
  • and reinterpret_cast<T&>(x) is equivalent to *reinterpret_cast<T*>(&x) (using builtin & and *) whenever the second cast is possible using the above rules.

(See Section 5.2.10 of the standard)

This means in particular that a cast from a pointer to function to void* is not possible, but you can cast it to void(*)().

share|improve this answer

If you're just looking to store different types of function pointer in a list then you can cast to a common function pointer type:

class Test {
  int a;

int main()
  Test* *p(void **a);
  void (*f)()=reinterpret_cast<void (*)()>(p);

This is valid to do via reinterpret_cast (5.2.10/6):

A pointer to a function can be explicitly converted to a pointer to a function of a different type. The effect of calling a function through a pointer to a function type (8.3.5) that is not the same as the type used in the definition of the function is undefined. Except that converting an rvalue of type "pointer to T1" to the type "pointer to T2" (where T1 and T2 are function types) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified.

share|improve this answer

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)));
share|improve this answer
Thanks for this — the code makes my eyeballs crawl, but it seems to work properly and avoid warnings/errors even with maxed out warning-levels in gcc and clang. –  snogglethorpe Dec 22 '11 at 7:21

shouldn't Test* *p(void **a); be Test* (*p)(void **a) ?

Maybe that is your problem?

share|improve this answer
Test* p(void a) is also resolving to type Test * ()(void).. so i think both representations are valid. Am sure of this because I tried that way too.. –  vprajan Aug 20 '09 at 12:07

Your Answer


