Allow MOLD in ALLOCATE to have different type from variable allocated

Since one may want to allocate an array to have the same shape as an array of a different type, I suggest that Fortran allow mold to have a different type from the variable being allocated. In the code below, the line marked not legal would be allowed.

program alloc
implicit none
integer :: ivec(3)
real, allocatable :: rvec(:)
allocate (rvec, mold = ivec) ! not legal
allocate (rvec, mold = real(ivec)) ! legal
print*,size(rvec)
end program alloc
3 Likes

If I recall correctly the gist of Fortran 202X edits (current draft here), the following will likely become conformant:

   ..
   integer :: n(2,3)
   real, allocatable :: x(:,:)
   ..
   allocate( x(shape(n)) )  !<-- likely to be permitted starting Fortran 202X
   ..

and if so, that should address the need in the original post here.

Basis: Fortran 202X shall contain the extension whereby “An ALLOCATE statement can specify the bounds of an array allocation with array expressions.”

3 Likes

I have repeatedly encountered this limitation in the past. An easy fix is to specify the size of allocation instead of mold. But specifying the size for multiple allocations of the same size becomes cumbersome. A fix like the suggestion in this post would enable multiple allocations with one size specification. An alternative would be to add a size argument to allocate statement. But that seems redundant given the presence of mold.

The multiple allocation scenario does not seem to be addressed by the Fortran 202X enhancement described in FortranFan’s comment.