Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28749 Discussions

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

avinashs
New Contributor I
841 Views

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
1 Solution
Steve_Lionel
Honored Contributor III
841 Views

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.

View solution in original post

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
842 Views

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.

0 Kudos
avinashs
New Contributor I
841 Views

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
Reply