Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

allocatable class(*) function return

Gorden_W_
Beginner
123 Views

So i have this piece of code:

module asd
    contains
        function return_alloc() result(res)
            class(*), dimension(:), allocatable :: res
            integer, dimension(3) :: x
            x = 5
            allocate(res, source = x)
            print *, transfer(res,(/1,1,1/))
        endfunction
        
        subroutine print_x()
            class(*), dimension(:), allocatable :: test
            allocate(test, source = return_alloc())
            print *, transfer(return_alloc(),(/1,1,1/)), "aa"
            print *, transfer(test,(/1,1,1/)), "bb", size(test)
        endsubroutine
endmodule
    
program xyxcxvxvcxcyc
    use asd
    call print_x()
endprogram

The output is:

          5           5           5
          5           5           5
aa
bb           3

while it shoud be:

          5           5           5
          5           5           5
          5           5           5 aa
          5           5           5 bb           3

So for some reason the function return is empty but retained it's dimensions.

my question now is obviously what am I doing wrong?

0 Kudos
4 Replies
FortranFan
Honored Contributor II
123 Views

IIRC, there is an open incident with Intel Fortran with the use of TRANSFER intrinsic in IO (e.g., PRINT statements) but presently I'm unable to locate any threads.  Intel Fortran team might be to find the details, if this is correct.

Steve_Lionel
Black Belt Retired Employee
123 Views

I have to ask - what is it you are really trying to do here? You seem to be abusing TRANSFER in my opinion.

At first I thought that it was not allowed for the SOURCE argument to TRANSFER be polymorphic, much less unlimited poiymorphic, but interp F03/0047 covered this and it is allowed. I think this should have worked as you desired.

Kevin_D_Intel
Employee
123 Views

I was unable to find other reports with TRANSFER in I/O but I’ll keep looking. For now, I did reproduce/report the findings here to Development.

(Internal tracking id: DPD200419455)

Gorden_W_
Beginner
123 Views

Then I also have found another interesting behaviour with this piece of code.

If I change line 14 to

print *, return_alloc(), "aa"

it compiles and gives the same output.

But if I change line 15 in the same manner, I get an error message saying i have to provide a user-defined I/O routine for polymorphic items, which should also apply to the above case.

Reply