【问题标题】:Automatic LHS reallocation with overloaded assignment重载分配的自动 LHS 重新分配
【发布时间】:2015-09-08 08:02:02
【问题描述】:

我有一个代码,当我使用 RHS 上的结构构造函数对 LHS 上的未分配可分配对象进行分配时,该代码会与我手头的所有编译器发生段错误。结构(派生类型)本身具有重载赋值。我认为应该在调用分配例程之前自动重新分配 LHS,但似乎并非如此。

在代码下方,演示问题。取消注释分配语句使一切正常,但我不明白为什么在这种情况下需要显式分配。有趣的是,如果我删除了重载的赋值,一切都会正常进行。

有什么提示吗?

module dummy
  implicit none

  type :: DummyType
    integer :: ii
  contains
    procedure :: assignDummyType
    generic :: assignment(=) => assignDummyType
  end type DummyType

  interface DummyType
    module procedure DummyType_init
  end interface DummyType

contains

  function DummyType_init(initValue) result(this)
    integer, intent(in) :: initValue
    type(DummyType) :: this
    this%ii = initValue
  end function DummyType_init

  subroutine assignDummyType(this, other)
    class(DummyType), intent(out) :: this
    type(DummyType), intent(in) :: other
    this%ii = other%ii + 1
  end subroutine assignDummyType
end module dummy

program test_dummy
  use dummy
  implicit none

  type(DummyType), allocatable :: aa

  !allocate(aa)   ! Should be covered via automatic reallocation...
  aa = DummyType(42)

end program test_dummy

【问题讨论】:

  • 没有看代码,但是你有什么版本的编译器?有些包含自动重新分配有点晚了。
  • 来自 GNU、Intel 和 NAG 的最新版本。但显然,这似乎是标准的问题,而不是编译器的问题,如下 IanH 所述。

标签: fortran fortran2003 fortran2008


【解决方案1】:

有一个recent discussion on comp.lang.fortran处理这个话题。

赋值语句要么是内在赋值,要么是定义赋值。内在赋值允许[重新]分配左侧,定义赋值不允许。

当您为分配通用标识符提供一个过程时,您的分配就是定义的分配。对应于左侧的参数的特性则要求分配左侧。

【讨论】:

  • 如果能加个链接就好了。
  • @IanH 哦,我明白了,谢谢!这意味着,如果使用库提供的类型,如果希望将其分配给未显式分配的可分配对象,则必须对该类型的实际实现有所了解(无论分配是否已被覆盖)。我觉得它不直观......如果你能提供关于 comp.lang.fortran 的讨论链接,我也将不胜感激。
  • 然后,为了您的库的用户,考虑将需要定义分配的类型包装在向客户端公开的另一种类型中。
猜你喜欢
  • 1970-01-01
  • 2012-08-02
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多