Fortran通用程序错误:可能不是通用的

时间:2015-03-07 19:01:08

标签: fortran procedure generic-programming fortran2003

我知道您可以使用抽象类型创建泛型过程,例如:

fortran class declaration of dummy argument

但我可以使用以下代码执行相同的操作吗?

module proc_mod
  public :: forced,ideal
  interface forced;    module procedure forced1;    end interface
  interface forced;    module procedure forced2;    end interface
contains

function forced1() result(forced)
 implicit none
 integer:: forced
 forced = 1
end function

function forced2(t) result(forced)
 implicit none
 integer,intent(in) :: t
 integer:: forced
 forced = t
end function

function ideal() result(i)
 implicit none
 integer:: i
 i = 2
end function

end module

program procTest
  use proc_mod
  implicit none

  integer :: n

  procedure(forced), pointer:: funPointer => NULL()

  write(*,'(A)') "Please enter the type of vortex calculation you wish to use."
  read(*,*) n                                                                  

  select case( n )
    case( 1 );    funPointer => forced
    case( 2 );    funPointer => ideal
    case default; funPointer => ideal
  end select

  write(*,'(A,I3)') "You chose function: ", funPointer()

  stop
end program

现在我收到错误:funPointer可能不是通用的。我确信它有办法,但我不熟悉通用程序。

1 个答案:

答案 0 :(得分:2)

不,您不能将过程指针指向通用过程。您也不能将通用过程作为伪参数传递。您必须始终选择一个特定的功能。

您或许应该解释一下您的意图,而不只是展示一些代码并询问是否可以采用不同的方式。但无论如何,请记住Fortran泛型用于编译时调度。

相关问题