【问题标题】:Overriding private function in different module覆盖不同模块中的私有函数
【发布时间】:2020-12-17 13:51:43
【问题描述】:

如果模块foo 中私有的类型绑定过程被第二个模块bar 中的类型覆盖(或试图被覆盖),如何解决?标准中有规范吗?考虑到以下示例代码,取决于编译器,打印 FOO(intel fortan 19.1.1)或 BAR(gfortran 7.5,也许更新的版本会给出不同的结果?),我想知道哪个是正确的。

module foo

type :: foo_t
contains
    procedure, private :: foobar
    procedure :: exec
end type foo_t

contains

    subroutine exec(obj)
        class(foo_t) :: obj
        call obj%foobar()
    end subroutine exec

    subroutine foobar(this)
        class(foo_t) :: this
        print *, "FOO"
    end subroutine foobar
end module foo

module bar
    use foo

    type, extends(foo_t) :: bar_t
    contains
        procedure :: foobar => impl
    end type bar_t

contains

    subroutine impl(this)
        class(bar_t) :: this
        print *, "BAR"
    end subroutine impl
end module bar

program test
    use foo
    use bar

    class(foo_t), allocatable :: inst
    allocate( bar_t :: inst)
    call inst%exec()
end program test

此外,是否可以在不同的模块中使用私有延迟方法扩展抽象类型,就像示例中的情况一样,如果 foobar 被延迟(仅使用上述 gfortran 编译器编译然后产生预期的结果,但问题是这是否也是正确的行为)?

【问题讨论】:

    标签: fortran polymorphism


    【解决方案1】:

    正确的结果是FOO

    这是 gfortran (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47805) 中的一个已知错误。

    在另一个模块中无法访问私有绑定。因为在模块 bar 中无法访问私有绑定,所以它不会被覆盖 (F2018 7.5.7.3p1)。 bar_t 中的绑定定义与 foo_t 中的绑定无关。

    (具有私有延迟绑定的抽象类型在定义它的模块之外不可用。有关详细信息,请参阅解释 F08/0052。)

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多