0

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++
5
  • I'm having a hard time grasping what the original code is doing (at a high level, not implementation level) so it's hard to suggest a C++ solution. Could you perhaps provide a minimal pseudocode example? Commented Mar 25, 2012 at 3:03
  • Why aren't you using classes and polymorphism? Commented Mar 25, 2012 at 3:03
  • Can you overload the function? One version accepts (for example) int *f1(int), and another version accepts (for example) int *f2(double, int). Commented Mar 25, 2012 at 3:08
  • @ApprenticeQueue Thanks. I will see, but what other options are there, I am looking for them. Commented Mar 25, 2012 at 3:08
  • @littleadv I am using auto fortran-to-c++ conversion tool and it just leaves few errors, which I am trying to solve. Fortran code is so big! Commented Mar 25, 2012 at 3:09

1 Answer 1

1

Generally speaking, this is a bad idea. Consider - what is myfunc going to do with this function pointer? What happens if you pass it abc_function_pointer, but it tries to call it with three ints? 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_pointer and xyz_function_pointer, so myfunc always calls a function with the same type and interface. You can insert a shim function that converts between the common interface and that of abc_function_pointer or xyz_function_pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't he get incompatible pointer type compile errors when trying to pass different function pointer types unless myfunc was overloaded to accept either?
@CollinHockey, indeed he would, and he apparently is according to the question comments. There are workarounds (function pointer casting), but these just move the problem from one that can be detected at compile time to one that may cause unpredictable behavior at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.