Binding C union in Fortran

Hi,

I have some C structures meant for interop with Fortran that contains unions (tagged ones). At the moment, as we are on Intel compiler we have access to the UNION extension but it is not standard and is only recognised by tools that implement them.
I saw there was equivalence and transfer but it says this is deprecated, very surprisingly.

Am I left with this compiler extension or is there something else that could work to allow the bindings?

Thanks
Regards

1 Like

Something which I imagine would work is

  1. Hide the whole struct and expose accessor functions that act on a pointer

Unions fall outside of the interoperable entities, so the classic approach to solve this is using indirection with an opaque pointer.


Neither are deprecated, however equivalence has been declared “obsolescent”. AFAICR, equivalence is limited to intrinsic numeric and character types, and sequence types, but not user-defined derived types.

I guess transfer could work, as long as you know the width of the union type. But it would still be a loophole:

The loophole lets the programmer breach the type-checking by the compiler. It is a way to say: “Don’t interfere, as I am smarter than the rules”.

Also, welcome to Fortran Discourse!

2 Likes

WG5 “conditionally accepted” a form of unions as proposal DIN02 for Fortran 202Y (see j3-fortran.org/doc/year/24/24-117.txt) I see that J3 was supposed to discuss this at the October 2024 meeting, but I don’t think we did. I’ll bring it up again.

2 Likes