fortran - 具有指向 CHARACTER(*) 变量的无限多态指针的 SELECT TYPE

标签 fortran gfortran fortran2003

以下示例使用 Fortran 2003 功能来定义无限多态指针,并根据 select type 构造之后的变量类型执行操作。子例程handleP根据参数的类型打印参数的值。

program example
    implicit none
    type name
        character(22) :: n
    end type

    character(len=7) :: mystring
    mystring = 'Initial'

    call handleP(mystring)
    call handleP('Initial')
    call handleP(name('Initial'))

    contains

        subroutine handleP(p)
            class(*), intent(in) :: p

            select type(p)
            type is (character(len=*))
                write(*,*) len(p), ': ', p
            class is (name)
                write(*,*) len(p%n), ': ', p%n
            class default
                write(*,*) 'Unknown type'
            end select
        end subroutine

end program example

使用 gfortran 版本 4.8 进行编译会给出以下输出:

       7 : Initial
       0 :
      22 : Initial

因此,使用 callhandleP(mystring) 一切都按预期工作,但使用 callhandleP('Initial') 打印失败。使用 type(name) 参数调用也可以。

call handleP('Initial') 的行为是 gfortran 错误还是我做错了什么?如果这是一个错误,我可以采取什么措施来防止它?

最佳答案

我只是想让大家知道这个问题已在当前 MinGW 安装附带的 gFortran 4.9.3-1 中得到解决。

关于fortran - 具有指向 CHARACTER(*) 变量的无限多态指针的 SELECT TYPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995529/

相关文章:

fortran - 使用 mpi_f08 模块时调用 mpi_gatherv 后数组的 Lbound 发生变化

vim - 为 pFUnit 测试框架扩展 vim Fortran 语法文件

从 gfortran 移植到 ifort 时出现编译错误

fortran - gfortran 中的 random_seed 导致内存泄漏?

fortran - Fortran 2003 中类型和类的区别

fortran - 如何根据变量的数据类型指定要执行的过程

c - MPI中如何获取物理机数量

compiler-errors - 为什么我不能分配意图?

integer - fortran 中的无符号整数是标准的吗?