void f(string str,int i )
{
cout<<str.c_str()<<endl;
cout<<i<<endl;
}
typedef void (*PF)(int i,string str);
int _tmain(int argc, _TCHAR* argv[])
{
PF pf=(PF)(void*)&f;
pf(10,string()); //runtime-error
return 0;
}
Due to some needs, I need to call the function f using the address of f which data type has been erased (such as the code above). But this solution may cause the insecure function call because the data type can’t be checked during the compiling.
Is there some ways to let the compiler to report an error when the the type of parameters are different from arguments’?
Something like this:
void f(T1 i,T2 j)
{
T1 p* = new i.real_type; //if i.real_type is different from T1, it will lead a compiling
....
}
I appreciate that very much.
The only run-time type information available in C++ is
typeidanddynamic_cast, and they only work on polymorphic types (class types with virtual functions). Function pointers don’t store the necessary information.Really, you should just avoid casting function pointers. The only place a function pointer case is ever necessary is on the return value from
GetProcAddressordlsym.