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 将泛型过程作为实际参数传递给函数_Fortran - Fatal编程技术网

Fortran 将泛型过程作为实际参数传递给函数

Fortran 将泛型过程作为实际参数传递给函数,fortran,Fortran,我试图将泛型过程作为实际参数传递给函数: module mymod implicit none interface func module procedure :: func1 module procedure :: func2 endinterface func contains real function func1(x) real,intent(in) :: x func1 = 2*x endfunction func1 real function func2(x,

我试图将泛型过程作为实际参数传递给函数:

module mymod
implicit none

interface func
  module procedure :: func1
  module procedure :: func2
endinterface func

contains

real function func1(x)
  real,intent(in) :: x
  func1 = 2*x
endfunction func1

real function func2(x,y)
  real,intent(in) :: x
  real,intent(in) :: y
  func2 = 2*x + 3*y
endfunction func2

real function func3(func,x,y)
  interface
    real function func(x,y)
      real,intent(in) :: x
      real,intent(in) :: y
    endfunction func
  endinterface
  real,intent(in) :: x
  real,intent(in) :: y
  func3 = func(x,y)
endfunction func3

endmodule mymod

program myprogram
use mymod
implicit none
write(*,*)func3(func,2.,3.)
endprogram myprogram
gfortran 6.2.0指出,我不能这样做:

test.f90:43:16:

 write(*,*)func3(func,2.,3.)
                1
Error: GENERIC procedure ‘func’ is not allowed as an actual argument at (1)
同样,对于ifort 17:

test.f90(39): error #8164: A generic interface name shall not be used as an actual argument.   [FUNC]
write(*,*)func3(func,2.,3.)
----------------^
test.f90(39): error #6637: When a dummy argument is a function, the corresponding actual argument must also be a function.   [FUNC]
write(*,*)func3(func,2.,3.)
----------------^
compilation aborted for test.f90 (code 1)

我正在阅读关于通用接口的2008年标准部分,我找不到这样的限制。我也想不出为什么编译器不能在编译时解析泛型接口。我的直觉告诉我这应该是可行的,但我可能没有正确的方法。您知道标准的兼容方法吗?

不,这是不允许的。实际上,您甚至不能将泛型内在函数作为伪参数传递

标准的兼容方式是直接使用正确的特定功能。对于内部函数,当特定函数没有标准名称时,有时必须为正确的类型编写包装器

例如:

  call integrate(derf,0.,1.)

  contains
    function derf(x)
      real(dbl) :: derf
      real(dbl), intent(in) :: x
      derf = erf(x)
    end function
  end

如果要传递双精度实数(或任何其他)版本的
erf()
,因为没有特定的函数可用。Fortran标准不允许将通用过程作为参数传递。为了传递内部函数/子例程,必须求助于用户定义的包装程序

module mymod

    ! Explicit typing only
    implicit none

    ! Declare procedure interface
    interface
       function my_func(x, y) result (return_value)
         real, intent(in) :: x, y
         real             :: return_value
       end function my_func
    end interface

contains

    function func1(x) result (return_value)
       real,intent(in) :: x
       real            :: return_value

       return_value = 2*x

    end function func1

    function func2(x, y) result (return_value)
       real, intent(in) :: x, y
       real             :: return_value

       return_value = 2*x + 3*y

    end function func2

    function func3(user_defined_func, x, y) result (return_value)
       procedure(my_func)  :: user_defined_func
       real, intent(in)    :: x, y
       real                :: return_value

       return_value = user_defined_func(x,y)

    end function func3

end module mymod

program main

    use ISO_Fortran_env, only: &
       stdout => OUTPUT_UNIT, &
       compiler_version, &
       compiler_options

    use mymod

    ! Explicit typing only
    implicit none

    write (stdout, *) func3(func2, 2.0, 3.0)
    write (stdout, *) func3(foo, 2.0, 3.0)
    write (stdout, *) func3(my_atan2, 2.0, 3.0)

    print '(/4a/)', &
    ' This file was compiled using ', compiler_version(), &
    ' using the options ', compiler_options()

contains

    ! A user defined function
    function foo(x, y) result (return_value)
       real, intent(in) :: x, y
       real             :: return_value

       return_value = 42

    end function foo

    ! A wrapper function to invoke the intrinsic atan2
    function my_atan2(x, y) result (return_value)
       real, intent(in) :: x, y
       real             :: return_value

       return_value = atan2(x,y)

    end function my_atan2

end program main
屈服

gfortran -std=f2008ts -o main.exe  mymod.f90 main.f90
./main.exe
   13.0000000    
   42.0000000    
  0.588002622    

 This file was compiled using GCC version 6.1.1 20160802 using the options -mtune=generic -march=x86-64 -std=f2008ts

我将指出,在Fortran 2015中,使用特定函数将变得不标准。此时,将无法将内部函数作为过程参数传递,必须使用包装器方法来保持标准兼容。(我还注意到,自Fortran 95以来,没有为内部函数添加新的特定名称。)