I am working on a C++ plugin that will be called from C#. The API I am trying to port comes with hundreds of functions and most of them are just overloads with the-same name but with different datatypes like int
, float
and char
.
Instead of writing the-same code but with different datatypes on both C++ and C# side over and over again, I want to use one function overload for it with a generic pointer. My goal is to use static_cast
from void*
to int
, float
and then char
and use which one that is successful first.
A simple test function from the C++ side:
void calculate(void* input1, void* input2)
{
float *i1 = static_cast<float*>(input1);
if(i1==NULL)
std::cout<<"Bad"<<std::endl;
else
std::cout<<*i1<<std::endl;
}
int main()
{
int input1 = 5;
int input2 = 10;
calculate(&input1,&input2);
return 0;
}
This is supposed to output "BAD" but it seems to be showing "7.00649e-45" which I believe is an undefined behavior. The static_cast
is failing and I don't know how check if it is successful or not. Checking if it is NULL
did not help in this case.
Is it possible to check if void*
to Object with static_cast
is successful or not? If so, how?
Note:
This is not a duplicate of this question. I don't want to find out the type of the Object from void*
. I just want check if the conversion or static_cast
is successful or not. That's it.