【发布时间】:2019-05-24 00:14:36
【问题描述】:
我认为子程序中显式形状伪参数数组的规范可以涉及任何整数变量,包括其他伪变量(通常情况),模块变量和当前子程序的局部变量。但事实证明,规范中不能使用局部变量(不是虚拟变量)。
一个例子如下:
module mp
implicit none
contains
subroutine p(b)
integer :: m=4, n=4 !not integer,parameter :: m=4, n=4
integer :: b(m,n)
end subroutine p
end module mp
gfortran 将引发Error: Variable 'm' cannot appear in the expression at (1)
对于这个例子,我可以使用integer,parameter :: m=4, n=4 来避免这种情况,但我不明白为什么原来的情况不起作用,考虑到在编译时不需要知道显式形状数组的边界/范围这一事实时间。上述示例的修改版本有效:
module mp
implicit none
integer :: m=4, n=4
contains
subroutine p(b)
integer :: b(m,n)
end subroutine p
end module mp
考虑到两个示例之间的细微差别,我希望它们都可以工作,但实际上前者没有。有人能解释一下原因吗?
更新:我发现这是一个非常微妙的问题,因为它取决于子例程是包含在模块中还是独立的,还取决于 gfortran 的版本。我已经在答案区域发布了示例。
【问题讨论】: