通过类型/种类/等级以外的方式区分 Fortran 中的泛型



大量使用非1索引的ALLOCATABLE数组,我希望它们的实际下限(因此也是上限)以它们作为IN/INOUT参数给出的过程而闻名(所以我将这些虚拟参数声明为延迟形状数组,以使它们与它们的边界一起传递;请参阅示例代码中的f_deferred_all)。

但。

有时这些过程(在数据声明部分之后)可以处理作为子数组的实际参数,因此当需要这样做时,我通常会复制该过程,更改其参数声明及其名称(参见过程f_assumed)。最好通过INTERFACE将两个程序放在相同的名称下.但是标准说我不能,因为

特定过程或绑定的接口由 固定其参数的属性,特别是类型,种类类型 参数、秩以及虚拟参数是否具有 POINTER 或 可分配属性。

在下面的示例程序中,我通过使用指针实际参数和虚拟参数克服了这个"限制"(请参阅f_deferred_ptr)。

  1. 这个"解决方案"是否以某种方式被弃用、气馁、危险或其他任何东西?我问这个是因为我真的没有那么多使用POINTER

此外,我定义了一个pntr函数,该函数将POINTER返回到其唯一(非ALLOCATABLE数组或数组的一部分)INput 参数,以便我可以"内联"使用它而无需在主程序中定义POINTER,并且我不必将TARGET属性放在实际参数上。

  1. 是否通过使用pntr创建了一个临时数组?我认为是的,但我不确定定义POINTER并将其关联到主程序中是否会在这方面产生影响。
  2. 是否有可能/是否计划/更改标准以允许泛型之间的这种区别是否有意义?如果这些建议有意义,在哪里发布这些建议?

下面是以下示例。

module mymod
implicit none
interface f
module procedure f_deferred_all, f_deferred_ptr!, f_assumed
end interface f
contains
integer function f_assumed(x) result(y)
integer, dimension(:), intent(in)  :: x
y = lbound(x,1)
end function f_assumed
integer function f_deferred_ptr(x) result(y)
integer, pointer,     dimension(:), intent(in) :: x
y = lbound(x,1)
end function f_deferred_ptr
integer function f_deferred_all(x) result(y)
integer, allocatable, dimension(:), intent(in) :: x
y = lbound(x,1)
end function f_deferred_all
function pntr(v) result(vp)
integer, dimension(:), target, intent(in)  :: v
integer, dimension(:), pointer             :: vp
vp => v
end function pntr
end module mymod
program prog
use mymod
implicit none
integer :: i
integer, dimension(-5:5)           :: v1
integer, dimension(:), allocatable :: v2
v1 = [(i, i = 1, 11)]
allocate(v2, source = v1)
print *, 'f_assumed(v1)', f_assumed(v1)
print *, 'f(pntr(v1))', f(pntr(v1(:))) ! is a temporary created?
print *, 'f(v2)', f(v2)
end program prog

您的示例代码不符合,因为F_DEFERRED_PTRF_DEFERRED_ALL的特定名称不明确。技术勘误表f08/0001将"12.4.3.4.5 对通用声明的限制"中的措词改为

Two dummy arguments are distinguishable if
• one is a procedure and the other is a data object,
• they are both data objects or known to be functions, and neither is TKR compatible with the other,
• one has the ALLOCATABLE attribute and the other has the POINTER attribute and not the INTENT(IN) attribute, or
• one is a function with nonzero rank and the other is not known to be a function.

这是由于指针,意图(IN)虚拟指针的实际参数可以是指针赋值语句中虚拟指针的有效目标(12.5.2.7指针虚拟变量J3/10-007r1)。指向问题讨论和解决方案的链接。

最新更新