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

User defined type as argument to elemental subroutine?


Hello everyone,

I have an elemental subroutine which is basically like this:

elemental subroutine calc_stuff(x, a, b, c)
   real, intent(in)  :: a, b, c
   real, intent(out) :: x

   x = a/b + c

end subroutine calc_stuff


Now I changed this to:

elemental subroutine calc_stuff(x, a, t)
   real,         intent(in)  :: a
   type(mytype), intent(in) :: t
   real,          intent(out) :: x

   x = a/t%b + t%c

end subroutine calc_stuff

Where "mytype" is a type containing various integer and real scalars, as well as a real allocatable array. The second code compiles fine on various compilers (GFortran, Intel, NEC, Cray), but now I saw that the Fortran standard says for elemental subroutines:
"All dummy arguments must be scalar, and must not have the ALLOCATABLE or POINTER attribute."

So, is my code not standard conforming and all the compilers "know" what I want but should actually complain, or am I misunderstanding the standard and everything is fine? It would be very good if I could use the second version, as I intend to branch the code where the calculation would then be something like "x = a/t%b + t%c/t%d" and I would not a have to change all calls to this function with the second version.

Thanks everyone in advance,

0 Kudos
1 Reply
Valued Contributor I

Your dummy argument mytype is nonpointer, nonallocatable, scalar data object, which can be intrinsic or derived type. So that is fine. The result variable is scalar and is neither a pointer nor allocatable, so that is also fine. You didn't show the definition of mytype, so one cannot see what t%b or t%c actually are, but they need to be scalars, but they could be either pointers or allocatables. So, your code is standard conforming AFAIC tell.


0 Kudos