cast to void* not working with function pointers
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 Casting a function pointer to another type
I was concerned about the below explanation.
Casting between function pointers and regular pointers (eg 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 ?
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 (ie 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:
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).
reinterpret_cast can only be used to
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(*)()
.
EDIT (2017): The answer above is only correct for C++03. In C++11 through C++17, it is implementation defined if conversions between function pointers and void *
are allowed. This is usually the case on POSIX compatible systems because dlsym()
is declared to return void *
, and clients are expected to reinterpret_cast
it to the correct function pointer type.
See cppreference.com for the full list of conversions allowed.
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.
链接地址: http://www.djcxy.com/p/73758.html