【发布时间】:2018-12-10 10:12:36
【问题描述】:
假设我们有以下代码:
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' 不能是 ASSUMED 或 DEFERRED
如果我把上面的代码改成
subroutine barsub (this)
class(bar(INT32,*)), intent(in) :: this
write (*,*) this%x
end subroutine barsub
两个编译器都能正常编译。
是否可以编写一个不需要明确指定种类参数的子程序?在上面的示例中,INT32、INT64、... 的代码是相同的,我不想为 kind 参数的每个可以想象的值复制粘贴它。它适用于 len 参数。为什么我不能对 kind 参数做同样的事情?
【问题讨论】:
-
派生类型的种类参数在这方面与内在类型相同。如果您了解这些规则,则可能没有太多要添加的内容了。
标签: fortran derived-types fortran2003