加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:54:18 所属栏目:百科 来源:网络整理
导读:我不知道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 type2end modul
我不知道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

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

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

我收到以下错误

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

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

解决方法

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

看着

type type2
  type(type1)          :: t1
  integer              :: x
end type type2

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

看着另一种类型

type type1
   type(type2),pointer :: t2
end type type1

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

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

C440 (R436) If neither the POINTER nor the ALLOCATABLE attribute is specified,the declaration-type-spec in the component-def-stmt shall specify an intrinsic type or a previously defined derived type.

我在这里把注意力限制在指针属性上.正如引用所暗示的那样,allocatable属性也是如此.允许以后使用allocatable引用是Fortran 2008中的新增功能:在Fortran 2003中,指针属性是必需的.正如Vladimir F所评论的那样,所有编译器都没有实现这种新的自由(提供递归可分配组件).在上面适当的读“指针或可分配”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读