tags:

views:

930

answers:

5

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 http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type

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 ?

+4  A: 

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).

AProgrammer
C cast worked perfectly. Thanks. :)
vprajan
(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
A: 

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

Maybe that is your problem?

leiz
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
+2  A: 

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.

Richard Corden
+3  A: 

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(*)().

Tobias
+1  A: 

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)));
}
Johannes Schaub - litb