Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
8 Views

Passing a pointer to a member function of a C++ class directly to Fortran using C interoperability

Jump to solution

Static functions in C are type compatible with Fortran functions and subroutines through the interoperability features. However, in C++, a static function does not have the same signature as the member function of a class although both may have the same prototype. Hence, a pointer to a static function in C++ cannot be made to point to a similar member function of a class. As a result, I am led to assume that the "c_f_pointer" in interoperability refers strictly to C-static functions. In fact, a quick test confirmed the same to be true.

Is there a way to actually pass a member function of a C++ class as a C-static function pointer to Fortran. This would greatly enhance the interoperability with C++ and help reuse numerical methods developed in Fortran directly with C++.

0 Kudos

Accepted Solutions
Highlighted
Black Belt
8 Views

Fortran's interoperability

Jump to solution

Fortran's interoperability features are strictly about C. To the extent that you can make C++ look like C (typically with 'extern "C"'), it works, but there is zero support for C++ features not in C.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
2 Replies
Highlighted
Black Belt
9 Views

Fortran's interoperability

Jump to solution

Fortran's interoperability features are strictly about C. To the extent that you can make C++ look like C (typically with 'extern "C"'), it works, but there is zero support for C++ features not in C.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
Highlighted
New Contributor I
8 Views

Quote:Steve Lionel (Ret.)

Jump to solution

Steve Lionel (Ret.) (Blackbelt) wrote:

Fortran's interoperability features are strictly about C. To the extent that you can make C++ look like C (typically with 'extern "C"'), it works, but there is zero support for C++ features not in C.

Thanks for this useful information - allows me to plan the software development accordingly. This effectively means that developing software in Fortran that requires a function or subroutine as an argument and is expected to be used in C++ will require C++ to provide a static function. Data required to compute the output of the static function will have to be passed either as additional arguments in Fortran or through global variables in C++. In some future version of C++ adopts the "contains" feature of Fortran, some of these issues could be resolved.

Some similar issues have also cropped up in using 64-bit Fortran DLLs with VBA in 64-bit Excel due to a change in the pointers required by VBA in the 64-bit version of Office. In short, functions and subroutines as arguments can complicate interoperability.

0 Kudos