fortran - 用户定义类型作为基本子程序的参数?
问题描述
在我的代码中,我有一个基本的子程序,基本上是这样的:
elemental subroutine calc_stuff (x, a, b, c)
real, intent(in) :: a, b, c
real, intent(out) :: x
x = a/b + c
end subroutine calc_stuff
我改成这样:
elemental subroutine calc_stuff (x, a, t)
real, intent(in) :: a
type(mytype), intent(in) :: t
real, intent(out) :: x
x = a/t%b + t%c
end subroutine calc_stuff
wheremytype
是一个包含一些标量real
and的类型integer
,以及一个real, allocatable
数组。成员b
和c
是real
s,使第二个版本与第一个基本相同。
第二个版本在各种编译器(Cray、Intel、NEC、GFortran)上编译得很好,但现在我读到元素子例程的标准状态:
所有虚拟参数都必须是标量,并且不能具有 ALLOCATABLE 或 POINTER 属性。
因此,在将用户定义的类型传递给元素子例程时,我的代码是否不符合标准,但所有编译器都“知道”我想要什么,因为我只使用类型中的标量而不是可分配数组?还是我误解了标准的措辞而第二版一切正常?
解决方案
虚拟参数t
是标量1,没有指针属性,也没有可分配属性。它不违反条件。
类型组件的属性与类型本身的属性无关。
1 Being of derived type doesn't make an object necessarily non-scalar. Even with multiple, or array components, the object itself may still be scalar. A derived type array is an array with element(s) of that type. Think also of a character object like character(len=3) name
: it is scalar but consists of multiple substrings.
推荐阅读
- c - 如何用 char 初始化矩阵。我正在尝试做公鸡游戏,一开始我需要一个带有“-”的矩阵 3x3
- html - 子菜单在手机上打不开
- python - sympy 函数中的隐式微分
- android - getHeight() 在第一次运行时没有返回正确的值-使用 ViewTreeObserver-
- node.js - 如何根据用户输入告诉 Alexa 从 LaunchRequest 跳转到特定意图
- vue.js - 如何检查我的 create-nuxt-app 版本并进行升级?
- python - 从字典中获取所有具有列表值的组合(来自嵌套字典列表的笛卡尔积)
- java - 在 Ubuntu 14.04 上从 Java 创建 Chrome 驱动程序失败:java.lang.IllegalStateException:驱动程序可执行文件不存在:
- python - 将类的实例分配给更大类的属性以创建嵌套点表示法
- python - setuptools pip wheel 失败,错误代码为 2