从接口访问参数(Fortran)

use*_*498 4 fortran

我正在使用一个参数来修复使用类型的精度.这工作正常,直到我尝试在界面中使用相同的类型.考虑这个小例子:

module Hello
    implicit none

    save
    integer, parameter  :: K = selected_real_kind(10)

contains

    subroutine dosomething(fun)
        real(K)     :: foo
        interface
           function fun(bar)
                real(K) :: bar
                real(K) :: fun
           end function fun
        end interface
    end subroutine

end module
Run Code Online (Sandbox Code Playgroud)

在这里,foo将是所需的类型,而编译器(gfortran)抱怨'bar'和'fun'.

错误是

Error: Parameter 'k' at (1) has not been declared or is a variable, which does 
not reduce to a constant expression
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个工作?(现在,我只是在处处编写selected_real_kind(10),但这根本不优雅)

谢谢!

M. *_* B. 7

最简单的方法是import在界面内添加.模块的定义超出了接口的范围,这在某种程度上是错误的设计.普通import将导入一切.

....
    subroutine dosomething(fun)
        real(K)     :: foo
        interface
           function fun(bar)
                import
                real(K) :: bar
                real(K) :: fun
           end function fun
        end interface
    end subroutine
....
Run Code Online (Sandbox Code Playgroud)

也可能: import :: K

  • 接口块的主要原因之一是为"在别处定义"的过程(例如外部过程)提供接口.其他地方定义的程序可能不知道接口块主机中定义的内容(它们可能没有相同的主机),因此将自动主机关联排除到接口主体对我来说似乎是合理的. (3认同)