【问题标题】:gfortran complains about rank of element in structure constructor for allocatable componentgfortran 抱怨可分配组件的结构构造函数中元素的等级
【发布时间】:2020-06-07 23:26:01
【问题描述】:

考虑以下代码:

subroutine tao_show_this ()
implicit none

type b_struct   
integer, pointer :: good_user => null()
end type

type a_struct   
 type (b_struct), allocatable :: value_ptr(:)
end type

type (a_struct) a
a = a_struct()

end subroutine

用 gfortran 5 或 7 编译得到:

gfortran -c test.f90
test.f90:4:13:

 type b_struct
             1
Error: The rank of the element in the structure constructor at (1) does not match that of the component (0/1)

这段代码用 ifort 编译得很好。这是 gfortran 错误还是我的代码有问题?

【问题讨论】:

标签: fortran gfortran


【解决方案1】:

对于默认结构构造函数,能够省略可分配组件的值是 Fortran 2008 中引入的一项功能。

gfortran 执行 not currently support this feature(“未实现的功能”)。

要让组件不被分配,同时仍然给构造函数一个值,引用null

a = a_struct(NULL())

作为 DavidS cmets,它以 a reported bug 的形式存在。

【讨论】:

  • 我不确定这是完整的答案,因为错误消息是关于排名不匹配而不是未初始化变量的问题。实际上,如果在上面的示例中将“value_ptr(:)”替换为“value_ptr”,则代码编译没有问题。另外,如果 b_struct 中的整数指针组件被一个简单的整数替换,编译没有问题。
  • 使用null 是否可以正常工作?错误消息确实令人困惑(当根本没有给出值时谈论具有标量值)但这可能只是缺乏正确的诊断以及缺乏支持。
  • 是的,它确实适用于 null。但是,如果 b_struct 被修改加上不正确的错误消息,代码在不使用 null 的情况下编译良好的事实向我表明,实际上 gfortran 旨在支持可分配组件的默认结构构造函数,这是一个“错误”。无论如何,我会将此报告给 gcc 人员,看看他们怎么说。
  • 与 gcc 的人交谈是一个不错的想法,但不幸的是,我目前无法测试一些想法,了解为什么有些事情可能会起作用。但是,我猜想我会认为可分配标量是 F2003 的添加而不是 F2008,测试等可能还不太兼容。
猜你喜欢
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 2019-07-19
  • 2019-06-03
  • 2017-06-12
  • 2020-08-29
  • 1970-01-01
  • 2018-02-09
相关资源
最近更新 更多