Fortran模块中类型之间的循环依赖关系

cre*_*ndo 4 fortran module

我不知道Fortran模块中究竟是如何解析循环依赖关系的.以下模块使用ifort-2016和gfortran-4.9进行编译

module types
   implicit none
   type type1
      type(type2), pointer :: t2
   end type type1

   type type2
      type(type1)          :: t1
      integer              :: x
   end type type2
end module
Run Code Online (Sandbox Code Playgroud)

但如果我将定义顺序更改为

module types
   implicit none
   type type2
      type(type1)          :: t1
      integer              :: x
   end type type2

   type type1
      type(type2), pointer :: t2
   end type type1
end module
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

error #6457: This derived type name has not been declared.   [TYPE1]
      type(type1)          :: t1
Run Code Online (Sandbox Code Playgroud)

ifort-2016和gfortran-4.9的行为相同.由于两个模块中存在类似的循环依赖性,为什么第一个编译但第二个不编译?

fra*_*lus 5

工作代码和不工作代码之间的区别在于具有pointer组件属性的类型的位置.这个属性允许类型声明引用先前未定义的类型.

看着

type type2
  type(type1)          :: t1
  integer              :: x
end type type2
Run Code Online (Sandbox Code Playgroud)

这里的type1类型组件type2没有pointer属性.这意味着该类型type1必须先前已定义.在你的第一个,工作,例子就是这样.在你的第二个,破碎的例子中,它不是.

看着另一种类型

type type1
   type(type2), pointer :: t2
end type type1
Run Code Online (Sandbox Code Playgroud)

type2组件具有指针属性.因此,type2在此引用之前不需要定义类型.您可以在两个示例中看到这一点.

此要求在Fortran 2008标准中说明为

C440(R436)如果既未指定POINTER也未指定ALLOCATABLE属性,则component-def-stmt中的declaration-type-spec应指定内部类型或先前定义的派生类型.


我在这里限制了对pointer属性的关注.正如引言所暗示的那样,allocatable属性也是如此.allocatable在Fortran 2008中允许使用后面的参考:在Fortran 2003中,该pointer属性是必需的.正如Vladimir F所评论的那样,所有编译器都没有实现这种新的自由(提供递归可分配组件).在上面的" pointerallocatable"中酌情说明.