Constant function pointer array in Fortran 2003

375 views Asked by At

Dear Fortran programmers,

does anybody know, whether it is possible to declare a constant (parameter) procedure pointer array in Fortran 2003 or above?

As given below, I have a switcher function, which calls different functions depending on an incoming integer argument. It uses an array of procedure pointers (wrapped in derived) types. This array has to be initialized via the init() routine during run time before it can be used. Is there any way, to initialize this array already during compilation and avoid the necessity of such an initialization routine? It could be then also defined as parameter, since it's value won't change during the run.

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
2

There are 2 answers

2
IanH On BEST ANSWER

Fortran 2008 permits initialization of procedure pointers and procedure pointer components to procedure targets (versus NULL()). The syntax is consistent with other initializers.

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

I don't think it is allowed. From my reading of the 2008 standard parameter is an attribute of a data object and the class of data objects does not include procedures or procedure pointers.

What did your compiler tell you when you fed it your code ?