I’m wondering if it’s safe to cast a double (*)(double) to double(*)(...), this is going to be used to generalize a code which may have pointers to multiple functions.
so far I’ve stored every thing that is going to be passed to the function in a vector, and I’m wondering if there is a way to call a function (while passing the correct number of arguments) in a generalized code? I mean something this:
//while initializing
mFunction = sin;
//later in code
double (*generalized)(...) = mFunction;
for(i=0;i<args.size();i++)
pusharg(args[i]);
call(generalized);
–edit–
if there is not a valid way to do it using c++ is it possible to saftly call the function using assembly?
You cannot directly assign a
double(*)(double)todouble(*)(...), but you couldreinterpret_castit. The cast is explicitly allowed by §5.2.10[expr.reinterpret.cast]/6, but calling the casted function pointer will cause undefined behavior:It is easy to see why it leads to UB — what if we call
generalized(1.0, 2.0, 3.0)? This will likely corrupt the call stack. But it is fine if you cast thegeneralizedback to adouble(*)(double)before you call it.