I try to construct a subroutine to reallocate memory for a type-independent allocatable array like this:
subroutine reallocate(vector, num)
implicit none
class(*), dimension(:), allocatable, intent(inout) :: vector
integer :: num
class(*), dimension(:), allocatable :: tmp
integer :: lb, ub, ii_
if (allocated(vector)) then
ub = max(ubound(vector, 1), ub)
lb = min(lbound(vector, 1), lb)
if (ub .GT. ubound(vector, 1) .OR. lb .LT. lbound(vector, 1)) then
allocate(tmp(ub:lb), source=vector)
tmp(lbound(vector,1):ubound(vector,1)) = vector
call move_alloc(tmp, vector)
else
return
end if
else
allocate(vector(num:num), source=vector)
return
end if
return
end subroutine
For example, let's say I have a type(type1), allocatable :: v allocated within the indices -1 and 4, and I call reallocate(v, 6). After that I want v to be allocated between -1 and 6.
So, problem here comes when vector is already allocated, and I want to keep the information already stored in the vector by copying it to a newly reallocated temporal array (line that reads tmp(lbound(vector,1):ubound(vector,1)) = vector). gfortran complains: "Error: Nonallocatable variable must not be polymorphic in intrinsic assignment at (1) - check that there is a matching specific subroutine for '=' operator."
Is what I intent allowed in the Fortran 2003 standard? What would be the way to do this?