I am converting fortran code to C++ and wanted to find a right option for function-pointer.
The fortran code is: for two different cases, it passes two different kinds of function-pointers to another function. These function pointers have different interfaces. In C++, I need to specify interface of function-pointer and hence it is not straightforward. Can someone suggest, if C++ functor or something else, that would be useful? I wish I could use something like ‘void (function) pointer’ and then ‘cast’, but I really don’t know. Thanks in advance.
EDIT: minimal example in fortran
Fortran
if(option1) then
call myfunc( abc_function_pointer,otherarguments)
else
call myfunc( xyz_function_pointer, otherarguments)
endif
where, these functions are (in C++)
void (*abc_function_pointer)(int, float) //in c++
void (*xyz_function_pointer)(int, int, int) //in c++
Generally speaking, this is a bad idea. Consider – what is
myfuncgoing to do with this function pointer? What happens if you pass itabc_function_pointer, but it tries to call it with threeints? Or vice versa?Without knowing what you’re using this for, it’s hard to suggest what you should do. Typically you would want to factor out the common interface between
abc_function_pointerandxyz_function_pointer, somyfuncalways calls a function with the same type and interface. You can insert a shim function that converts between the common interface and that ofabc_function_pointerorxyz_function_pointer.