@FedericoPerini ,
As pointed out to you by @shahmoradi , your use of assumed-size array is really buying you little. You can view an assumed-size array as effectively a 1-D target that you can reshape to pointers of different ranks with the onus of managing the shape still remaining on you as a programmer. Thus you as a coder end up forcing the generic resolution via shape management. Instead you can have a single procedure with an assumed-shape array argument with TARGET
attribute and achieve the same purpose via local pointers. Note with assumed-size received arguments of rank greater than 1, it is explicit-shape in all dimensions except the last, thus why bother …
Separately, you can do as advised by @RonShepard and use array slices (or vector subscripts) as a clearer way to “consume” your generic procedure if you need to persist with that option.
module m
private
generic, public :: sub => sub1, sub2
contains
subroutine sub1( n, x )
integer, intent(in) :: n
real, intent(in) :: x(*) !<-- assumed-size only in the last dimension
print *, x(1:n)
end subroutine
subroutine sub2( n, m, x )
integer, intent(in) :: n
integer, intent(in) :: m
real, intent(in) :: x(m,*) !<-- assumed-size only in the last dimension; explicit-shape otherwise
print *, x(1:m,1:n/m)
end subroutine
end module
use m
real, allocatable :: a(:)
real, allocatable :: b(:,:)
a = [ 0, 0, 1, 2, 0 ]
b = reshape( [( real(i), i = 1, 12 )], shape=[4,3] )
call sub( n=2, x=a(3:4) ) !<-- say data of interest in only locations 3 and 4
call sub( n=3*2, m=3, x=b(1:3,1:2) ) !<-- say data of interest are in 1:3, 1:2
end
C:\temp>ifort /standard-semantics p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.7.0 Build 20220726_000000
Copyright (C) 1985-2022 Intel Corporation. All rights reserved.
Microsoft (R) Incremental Linker Version 14.33.31630.0
Copyright (C) Microsoft Corporation. All rights reserved.
-out:p.exe
-subsystem:console
p.obj
C:\temp>p.exe
1.000000 2.000000
1.000000 2.000000 3.000000 5.000000 6.000000
7.000000