在Fortran数组中删除变量? [英] Variables being deleted in Fortran Arrays?

查看:134
本文介绍了在Fortran数组中删除变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中包含抽象类型,继承类型和简短程序,其中我在其中创建对象并将其存储在数组中.

I have a following code, with an abstract type, inherited type and a short program, where I'm creating an object and storing it in an array.

module m
    implicit none

    type :: container
        class(a), allocatable :: item
    end type container

    type, abstract :: a
        integer, public :: num
    end type a

    type, extends(a) :: b
        integer, public :: num2
   end type b
end module m

program mwe
    use m

    implicit none

    class(a), allocatable :: o1
    class(container), allocatable :: arr(:)

    o1 = b(1, 2)

    allocate(arr(2))
    arr(1) = container(o1)

    select type(t => o1)
        type is(b)
        write(*,*) t%num, t%num2
    end select

    select type(t => arr(1)%item)
        type is(b)
        write(*,*) t%num, t%num2
    end select
end program mwe

问题是,输出看起来像这样:

The problem is, that the output looks like this:

       1           2
       1           0

可以看出,存储在数组中的同一变量的第二个变量为空.为什么会这样呢?是因为数组的类型为 a ,仅包含第一个变量?

As can be seen, the same variable stored in the array has the second variable nullified. Why is that happening? Is it because the array is of type a, which only contains the first variable?

我正在使用 ifort版本18.0.3 编译代码.

推荐答案

与maturero的 answer 程序输出有效.但是,我们可以对代码进行简单的修改以使其正确使用Fortran. 1 此答案与此修改的版本有关.

As with ripero's answer one could say that any output from the program is valid. However, we can make a simple modification to the code to make it correct Fortran.1 This answer is concerned with this modified version.

我将这种意外的输出称为编译器供应商的帮助.

I would call this unexpected output and seek the help of the compiler vendor.

使用具有多态可分配组件的结构构造函数是Fortran中的那些新领域之一.编译器可能需要一些时间才能赶上或正确执行.

Using a structure constructor with polymorphic allocatable components is one of those new areas in Fortran. Compilers may take a while to catch up or do it correctly.

我已经使用Intel Fortran 18.0.2测试了您的代码,并看到了相同的输出.

I have tested your code with Intel Fortran 18.0.2 and see the same output.

您的问题

是因为数组的类型是 a ,它仅包含第一个变量?

Is it because the array is of type a, which only contains the first variable?

否:在 select type 部分中,输出为 t 的是类型为 b 的非多态实体.

No: in the select type part with the output t is a non-polymorphic entity of type b.

您可以通过避免使用结构构造函数来解决此问题:

You may work around this problem by avoiding using the structure constructor:

arr(1)%item = o1

我还看到18.0.2之前的Intel编译器仍然做一些不同的事情.

I also see that Intel compilers before 18.0.2 do something different still.

1 带有声明

    class(container), allocatable :: arr(:)

arr 是多态的且可分配.如成熟者所言,这意味着 arr(1)( arr 的元素)是多态的.但是,作为数组元素, arr(1)本身并不是多态的,因此可能不在内部赋值语句的左侧.我们可以通过两种方式更改代码:提供定义的赋值,或者使 arr 不是多态的.在问题代码中,似乎没有理由让容器具有多态性,所以我会考虑

arr is polymorphic and allocatable. As ripero notes, this means that arr(1), the element of arr is polymorphic. However, as an array element, arr(1) is not itself polymorphic and so may not be on the left-hand side of an intrinsic assignment statement. We can change the code in two ways: provide defined assignment, or make arr not polymorphic. In the code of the question there seems no reason to have the container polymorphic, so I'll consider

type(container), allocatable :: arr(:)

进一步,如对该问题的评论中所述,如果您希望使用gfortran 8或更早的版本,以查看会发生什么,还应该修改问题中的代码,以使派生类型的定义容器位于派生类型 a 的定义之后.

Further, as discussed in comments on the question, if you wish to work with gfortran 8, or earlier, to see what happens, you should also modify the code in the question so that the definition of the derived type container comes after the definition of the derived type a.

这篇关于在Fortran数组中删除变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆