Fortran 2003 中的常量函数指针数组

Constant function pointer array in Fortran 2003

亲爱的 Fortran 程序员,

有谁知道,是否可以在 Fortran 2003 或更高版本中声明常量(参数)过程指针数组?

如下所示,我有一个切换器函数,它根据传入的整数参数调用不同的函数。它使用一组过程指针(包装在派生中)类型。该数组必须在 运行 时间 期间通过 init() 例程初始化,然后才能使用。有什么办法可以在 编译 期间初始化这个数组并避免这种初始化例程的必要性?它也可以定义为 parameter,因为它的值在 运行.

期间不会改变
module testmod
  implicit none

  interface
    function funcInterface() result(res)
      integer :: res
    end function funcInterface
  end interface

  type :: ptrWrap
    procedure(funcInterface), nopass, pointer :: ptr
  end type ptrWrap

  type(ptrWrap) :: switcher(2)

contains

  subroutine init()
    switcher(1)%ptr => func1
    switcher(2)%ptr => func2
  end subroutine init

  function callFunc(ii) result(res)
    integer, intent(in) :: ii
    integer :: res
    res = switcher(ii)%ptr()
  end function callFunc

  function func1() result(res)
    integer :: res
    res = 1
  end function func1

  function func2() result(res)
    integer :: res
    res = 2
  end function func2

end module testmod


program test
  use testmod
  implicit none

  call init()  ! I'd like to get rid of this call.
  print *, callFunc(1)
  print *, callFunc(2)

end program test

我认为这是不允许的。根据我对 2008 年标准的阅读,parameterdata object 的属性,数据对象的 class 不包括过程或过程指针。

当您向编译器输入代码时,您的编译器告诉您什么?

Fortran 2008 允许将过程指针和过程指针组件初始化为过程目标(相对于 NULL())。语法与其他初始化程序一致。

! In the scope of the module.
...
type(ptrWrap), parameter :: switcher(2) = [ptrWrap(func1), ptrWrap(func2)]
...