gfortran成员析构函数
我注意到gfortran(GNU Fortran(Debian 4.9.2-10)4.9.2)有一个奇怪的链接错误,这取决于我在派生类型中定义成员的顺序。使用ifort(ifort(ifort)18.0.1 20171018),代码的编译和行为符合预期gfortran成员析构函数,fortran,gfortran,derived-types,Fortran,Gfortran,Derived Types,我注意到gfortran(GNU Fortran(Debian 4.9.2-10)4.9.2)有一个奇怪的链接错误,这取决于我在派生类型中定义成员的顺序。使用ifort(ifort(ifort)18.0.1 20171018),代码的编译和行为符合预期 module bug implicit none type indestructable integer :: i end type type destructable integer :: i contai
module bug
implicit none
type indestructable
integer :: i
end type
type destructable
integer :: i
contains
final :: destruct
end type destructable
type compound
type(destructable) :: des
type(indestructable) :: ind
end type compound
contains
subroutine destruct(instance)
type(destructable), intent(in) :: instance
write(*,*) instance%i
end subroutine destruct
subroutine run
type(compound) :: cmp
cmp%des%i = 3
cmp%ind%i = 4
end subroutine run
end module bug
program main
use bug, only: run
implicit none
call run
end program main
此程序应在结束时打印出“3”,因为“cmp”中的“des”有一个析构函数,该析构函数写出其成员“i”,该成员设置为3
在gfortran中,编译器给出一个错误,即没有定义复合类型的析构函数。这个析构函数应该自动生成并调用所有成员的析构函数。问题是复合类型中还有一个类型的成员没有析构函数。这在某种程度上阻碍了gfortran组织破坏者
解决这个问题的方法是将可破坏成员放在不可破坏成员之后(在component的类型定义中切换两行)
是否有人知道这是一个编译器问题,可能会在以后的版本中解决,或者我做错了什么,ifort以某种方式为我修复了它。我总是知道,成员变量的定义顺序应该无关紧要
对于遇到同样问题的人:“始终将可销毁的成员放在末尾”。然而,非派生类型似乎并不重要,即使它们是可分配的。这很可能在GCC 7中得到解决。更新你的GCC。我可以确认您的代码在此版本中编译。可能是上述版本中的一个错误(GCC 4.9.2发布[2014-10-30]),我尝试在Windows上使用gfortran 7.3.0(因此没有真正的可比性),但我没有看到问题。gfortran应该还没有。gfortran中未最终确定的主要内容是函数结果,但这里不使用这些。