Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 2003中的常量函数指针数组_Fortran_Fortran2003_Fortran2008 - Fatal编程技术网

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

Fortran 2003中的常量函数指针数组,fortran,fortran2003,fortran2008,Fortran,Fortran2003,Fortran2008,亲爱的Fortran程序员: 有人知道,在Fortran 2003或更高版本中是否可以声明常量(参数)过程指针数组 如下所示,我有一个switcher函数,它根据传入的整数参数调用不同的函数。它使用一个过程指针数组(封装在派生类型中)。此数组必须在运行时通过init()例程初始化,然后才能使用。是否有任何方法可以在编译过程中初始化此数组并避免需要这样的初始化例程?它也可以被定义为参数,因为它的值在运行期间不会改变 module testmod implicit none interfa

亲爱的Fortran程序员:

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

如下所示,我有一个switcher函数,它根据传入的整数参数调用不同的函数。它使用一个过程指针数组(封装在派生类型中)。此数组必须在运行时通过
init()
例程初始化,然后才能使用。是否有任何方法可以在编译过程中初始化此数组并避免需要这样的初始化例程?它也可以被定义为
参数
,因为它的值在运行期间不会改变

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标准
的理解来看,参数
数据对象的一个属性
,数据对象的类别不包括过程或过程指针


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

Fortran 2008允许初始化过程指针和指向过程目标的过程指针组件(而不是
NULL()
)。语法与其他初始值设定项一致

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

这也是我的阅读。如果将
参数
属性添加到过程声明语句中,gfortran将发出错误
参数属性与指针属性冲突
。已尝试创建常量过程指针,如
过程(funcInterface),指针,parameter::pFunc1=>func1
与我尝试过的所有编译器一起失败,表明指针和parameter属性之间存在冲突。但不确定,为什么,因为它是一个常量,在运行期间不会改变,目标是一个模块过程,在声明的范围内。我认为,从根本上说,你是在问一个物种的问题,为什么语言X没有功能Y?即使有可能提供权威和正确的答案,您仍然无法在X中执行Y。虽然过程指针不是数据对象,但具有过程指针组件的派生类型的对象是数据对象。@HighPerformanceMark您是正确的,这更像是一个语言庸俗的问题。我想了解,如果有任何基本原因,为什么Fortran中不允许使用它(例如,模块过程不被识别为常量实体)。在我看来,Fortran是一种逻辑性很强的后续语言,通常在被禁止的事情背后总会有一个很好的解释。:-)这似乎正是我想要的。不幸的是,到目前为止,我所有的编译器(intel15、nagfor 6.0、gfortran 5.0)都无法处理它。例如,NAG抱怨切换器的初始化表达式不是常数。由于某些原因,他们似乎无法将模块过程识别为常量实体。请注意,这是Fortran 2008的一项功能。这些编译器目前都没有完全实现Fortran 2008。几年后再试一次。