Fortran 中的接口(interface)类型绑定(bind)过程

标签 interface fortran type-bounds

我正在尝试定义 interface d 过程作为 Fortran 中的类型绑定(bind)过程 type定义,但它似乎不像人们期望的那样工作。考虑以下模块:

module example_module
implicit none
private

interface add_them
  module procedure add_them_integer,add_them_real
end interface add_them

type, public :: foo
  integer, private :: a=1,b=2
  real, private :: c=4.,d=5.
contains
  procedure, public :: add => add_them
end type foo

contains
subroutine add_them_integer(self,x)
class(foo), intent(in) :: self
integer, intent(in) :: x
print *,self%a+self%b+x
end subroutine add_them_integer

subroutine add_them_real(self,x)
class(foo), intent(in) :: self
real, intent(in) :: x
print *,self%c+self%d+x
end subroutine add_them_real
end module example_module

以及使用该模块的相应程序:
program example
use example_module
implicit none
type(foo) :: foofoo
call foofoo%add(1)
call foofoo%add(2.)
end program example

我希望它可以编译,结果应该是 4 和 11。但是,gfortran 报告以下错误:
procedure, public :: add => add_them
         1
Error: 'add_them' must be a module procedure or an external procedure with an explicit interface at (1)

一种解决方法是使用 generic类型绑定(bind)过程而不是 interface d 一,使模块如下:
module example_module
implicit none
private

type, public :: foo
  integer, private :: a=1,b=2
  real, private :: c=4.,d=5.
contains
  generic, public :: add => add_them_integer,add_them_real
  procedure, private :: add_them_integer,add_them_real
end type foo

contains
subroutine add_them_integer(self,x)
class(foo), intent(in) :: self
integer, intent(in) :: x
print *,self%a+self%b+x
end subroutine add_them_integer

subroutine add_them_real(self,x)
class(foo), intent(in) :: self
real, intent(in) :: x
print *,self%c+self%d+x
end subroutine add_them_real
end module example_module

这按预期工作。但是,我不能使用 generic程序。以上只是演示问题的简化示例,但在我的实际代码中,'add_them' 不能是 generic过程,因为“foo”实际上是派生类型,而“add_them”覆盖了父类型中定义的过程; gfortran(至少)不允许 generic覆盖基本过程的过程。为了绕过这个限制,我想我应该使用 interface相反,但正如您在上面的示例中看到的那样,虽然 'add_them' 定义正确,但编译器提示“'add_them' 必须是模块过程或具有显式接口(interface)的外部过程”。

任何帮助,将不胜感激;提前致谢。

最佳答案

您的第一段代码的 gfortran 错误是正确的。进行通用绑定(bind)的方法是按照您的“按预期工作”部分的代码。

如果父类型具有特定名称的特定绑定(bind),则您不能在扩展中重用该名称,除非覆盖特定绑定(bind)。

如果你想要add (注意名称 add_them 没有出现在您的第二种情况中)成为扩展中的通用绑定(bind),然后使其成为父级中的通用绑定(bind)。

关于Fortran 中的接口(interface)类型绑定(bind)过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436475/

相关文章:

java - 正在寻找计算大型矩阵并输出它们的最快方法?

syntax - 在结构类型中包装 RefCell 和 Rc

scala - 如何在 Scala 中设置多个 ORed 类型边界

scala - 类型上限允许子类型但不允许父类型

regex - 使用 IFn 扩展 Clojure 正则表达式以支持映射

Java-Interface 使用接口(interface)

java - 无法在 Android 中使用 BroadcastReceiver 上下文初始化接口(interface)

winapi - 在 Rust 中实现 IWbemObjectSink 接口(interface)

python - 无法从 ctypes 获取 Fortran 函数输出

fortran - 定义派生数据类型数组的默认值