假设我们有以下代码:
module foo
use :: iso_fortran_env
implicit none
type :: bar (p, q)
integer, kind :: p
integer, len :: q
integer(kind = p), dimension(q) :: x
end type bar
contains
subroutine barsub (this)
class(bar(*,*)), intent(in) :: this
write (*,*) this%x
end subroutine barsub
end module foo
这段代码在gfortran 8和pgfort 18.4编译器下均无法编译。PGI编译器报错:
非法选择器-KIND值必须为非负数 假定类型参数(*)不能与非长度类型参数p一起使用
而gfortran则会出现以下错误:
在(1)处,KIND参数“p”既不能被假定也不能被推迟
如果我将上述代码更改为
subroutine barsub (this)
class(bar(INT32,*)), intent(in) :: this
write (*,*) this%x
end subroutine barsub
这段代码可以在两个编译器上编译通过。
是否可能编写一个子程序,其中kind参数不需要显式指定?在上面的示例中,对于INT32
,INT64
等,代码将相同,我不想为每个可想象的kind参数值复制粘贴它。len参数可以正常工作。为什么我不能对kind参数执行相同的操作?