Binding C union in Fortran

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