4

Is there any way to automatically initialize a constant array of procedure pointers?

I have a bunch of routines, which have to be called depending on the value of an integer variable. Instead of using a select case statement, I would like to use procedure pointers as given below. However, it would be nice, if I could skip the explicit initialization of the procedure pointer array, and just define it as a constant array of wrapped procedure pointers. The code below demonstrates the solution I've found, the commented lines indicate the goal, I'd like to achieve:

module testmod
  implicit none

  abstract interface
    subroutine subInterface()
    end subroutine subInterface
  end interface

  type :: SubPtr
    procedure(subInterface), nopass, pointer :: ptr
  end type SubPtr

  ! Would be nice to use something like this:
  !type(SubPtr), parameter :: subs(2) = [ SubPtr(sub1), SubPtr(sub2) ]

contains

  subroutine sub1()
    print *, "SUB1"
  end subroutine sub1

  subroutine sub2()
    print *, "SUB2"
  end subroutine sub2

end module testmod


program test
  use testmod
  implicit none

  type(SubPtr) :: subs(2)
  integer :: ii

  ! Would be nice to get rid of those two initialization lines
  subs(1) = SubPtr(sub1)  
  subs(2) = SubPtr(sub2)

  ! Testing procedure pointer array
  do ii = 1, 2
    call subs(ii)%ptr()
  end do

end program test
Bálint Aradi
  • 3,754
  • 16
  • 22
  • I was able to get rid of most of the error messages by placing the subroutines in a different module. Still, the Cray compiler reports: `The initialization expression must be a constant to be used with PARAMETER assignment for object "SUBS".` Initializing a standalone procedure pointer to one of those subroutines works well. – Vladimir F Героям слава Oct 02 '15 at 09:19
  • 2
    Initializing a standalone procedure pointer to one of those subroutines works well, but nut for a `PARAMETR` procedure pointer! For the compiler it is a syntax error to use `PARAMETER` in procedure pointer context, I don't have time to check the constraints in the standard, but it probably is not permitted to have constant procedure pointers and initialize them to an existing subroutine. – Vladimir F Героям слава Oct 02 '15 at 10:16

1 Answers1

1

I think this (your commented out "Would be nice..." type declaration statement) simply requires (working) Fortran 2008 support. To be valid in a constant expression the component in a structure constructor corresponding to a pointer component may be an initialization target (7.1.12p1 3b). For a procedure pointer component, an initialization target is an initial-proc-target, which permits (amongst other things) a non-elemental module procedure, which is what sub1 and sub2 both are.

IanH
  • 21,026
  • 2
  • 37
  • 59