如何在 Fortran 派生类型中存储对过程的引用

85f*_*fce 4 fortran

我可以在 Fortran 类型中存储对过程的引用吗?

我的目标是通过将重复的参数分组为一个类型来减少 Fortran 子例程中的重复参数。但是 Fortran 不允许我对外部过程执行此操作。

这是我尝试做的一个简化示例:

module my_functions
    type mytype
        external :: f
    end type
contains
    subroutine fa()
        WRITE(*,*) "yiha"
    end subroutine

    subroutine fb(t)
        type(mytype) t
        call t%f()
    end subroutine
end module

program test
    use my_functions
    type(mytype) :: m
    m%f = fa
    call fb(m)
end program
Run Code Online (Sandbox Code Playgroud)

然而 gfortran 给了我

     external :: f
                 1
Error: Unexpected attribute declaration statement at (1)
Run Code Online (Sandbox Code Playgroud)

fra*_*lus 5

派生类型可以将过程指针作为组件:

implicit none

type mytype
  procedure(), pointer, nopass :: f
end type

type(mytype) m

external fa
m%f => fa

call m%f()

end
Run Code Online (Sandbox Code Playgroud)

该类型有一个带有隐式接口的过程,稍后将其作为子例程引用。由于它具有隐式接口,因此nopass指针需要该属性。