The following code compiles clean on GCC but gets an error on Clang:
typedef void (MyFuncPtr)();
void foo(const MyFuncPtr* ptr)
{
MyFuncPtr* myTestPtr = ptr;
}
Clang error:
error: cannot initialize a variable of type 'MyFuncPtr *' (aka 'void (*)()') with an lvalue of type 'const MyFuncPtr *'
(aka 'void (const *)()')
I have tried the following solutions and they all get errors except for the C-style cast:
const_cast:
MyFuncPtr* myTestPtr = const_cast<MyFuncPtr*>(ptr);
Error:
error: const_cast to 'MyFuncPtr *' (aka 'void (*)()'), which is not a reference, pointer-to-object, or pointer-to-data-member
reintepret_cast:
MyFuncPtr* myTestPtr = reinterpret_cast<MyFuncPtr*>(ptr);
Error:
error: reinterpret_cast from 'const MyFuncPtr *' (aka 'void (const *)()') to 'MyFuncPtr *' (aka 'void (*)()') casts away
qualifiers
C-style cast:
MyFuncPtr* myTestPtr = (MyFuncPtr*) ptr;
Success!
Questions:
Why doesn't const_cast work on function pointers?
Is using a C-style cast the only solution?
Why does this work on GCC with no casting?
Thanks in advance!
COMPILER VERSIONS:
*G++ version 4.6.3
*clang version 3.5.0.210790