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

Fortran and C++ reference types

Martin__Paul
New Contributor I
542 Views

I have a C++ function called by Fortran which passes arguments by reference. The parameters in the C++ function are reference types (int& var) and the program compiles and runs without error. I can't use value types for the dummy arguments, whereas in C++ the following value assignment to a reference is legal:

int var = 0;

int &var = var;

All examples I have found which describe passing by reference use C-style pointers in C/C++ interfaces. Is the use of reference types discouraged (presumably so in case a null argument is passed)?

Thanks.

0 Kudos
5 Replies
Jugoslav_Dujic
Valued Contributor II
542 Views
Quoting pdm2009

All examples I have found which describe passing by reference use C-style pointers in C/C++ interfaces. Is the use of reference types discouraged (presumably so in case a null argument is passed)?

In one word -- no. I use references all the time in C++ (well, every time they're viable), and interface them with Fortran heavily. I suppose pointers are a heritage of C proper in this area, but you should be just fine with references.

0 Kudos
jimdempseyatthecove
Honored Contributor III
542 Views
A C++ reference as an argument is fundamentally the same as a pointer (stack contents are the same). However, the code within the C++ function containing the reference argument is written in a style as if the referenced variable were declared within the scope of the function. IOW foo.mvar is use instead of foo->mvar. This permits you to cut and paste code without changing "." to "->" which could get you in trouble if you happen to error on your find and replace. On the FORTRAN side, pass theseC++referencearguments by reference and you should be ok.

Jim

0 Kudos
Martin__Paul
New Contributor I
542 Views

Thanks - I'll continue to use references.

0 Kudos
IanH
Honored Contributor II
542 Views
C interoperability is formally between C and Fortran. C doesn't have references. To be formally correct, when you work between C++ and Fortran, via the common C intermediary, you work with pointers, as that's what the standards guarantee will work (C++/C compatibility is covered by the C++ standard, C/Fortran compatibility by the Fortran standard).

In all implementations I'm aware of C++ references are implemented as a C pointer behind the scenes, though I've not found a formal requirement for that in the C++ standard (I'm no expert here - if someone knows better, please point me to it). Practically, you can therefore assume that references and pointers "interoperate". I guess there's always the infinitessimally small chance that a compiler vendor with a pathological bent might decide to trip you up one day but I wouldn't lose any sleep over it.
0 Kudos
jimdempseyatthecove
Honored Contributor III
542 Views
IanH,

On one side of the fence you could concievably have an interpreter (not the original poster's case) where the interpreter uses token references (e.g. an index or key or name) that is not a pointer. While the code on the interpreter side could reach the data by way of the token, the other side (non-interpreter) could not. Explicitly using pointers on the C (C++) side could avoid a portability issue by explicitly stating the argument is "address of".

Pointers can get you in trouble too. e.g. a non-SMP system where processors may not necessarily have the same word length or other properties (different address space such as on Harvard archetecture).

Jim Dempsey
0 Kudos
Reply