以种类参数作为子例程参数的参数化派生类型

Parametrized derived types with kind parameter as subroutine argument

假设我们有以下代码:

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 编译器说

Illegal selector - KIND value must be non-negative Assumed type parameter (*) cannot be used with non-length type parameter p

而 gfortran 产量

The KIND parameter 'p' at (1) cannot either be ASSUMED or DEFERRED

如果我将上面的代码更改为

subroutine barsub (this)
    class(bar(INT32,*)), intent(in) :: this
    write (*,*) this%x
end subroutine barsub

两个编译器都能正常编译。

是否可以编写一个不需要明确指定 kind 参数的子程序?在上面的示例中,INT32INT64、...的代码是相同的,我不想为 kind 参数的每个可以想象的值复制粘贴它。它适用于 len 参数。为什么我不能对 kind 参数做同样的事情?

Is it possible to write a subroutine where the kind parameter does not need to be specified explicitly?

否,kind 类型参数需要由常量表达式给出或默认,参见例如 Fortran 2008 标准,定义 1.3.147.12.3。

Why can't I do the same with the kind parameter?

lenkind 类型参数具有不同的用途和要求这一事实是具有两种类型参数的关键,如果它们的特性相同,我们就不需要两个他们。

请注意,过程需要它们的参数化派生类型的虚拟参数的 kind 参数,就像它们需要它们的内部类型的虚拟参数的 kind 一样:定义它们的值在编译时。