D
Dave Townsend
Hi,
I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-
1. What is the sanctified way to write this type of code.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).
Thanks,
dave.
#include <stdio.h>
static int foo()
{
printf("hello\n");
return 0;
}
typedef int fnc_t();
int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;
fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux
fnc();
return 0;
}
I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-
1. What is the sanctified way to write this type of code.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).
Thanks,
dave.
#include <stdio.h>
static int foo()
{
printf("hello\n");
return 0;
}
typedef int fnc_t();
int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;
fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux
fnc();
return 0;
}