types - Fortran: 'select type'子句中​​的参数化派生类型

标签 types polymorphism fortran parameterized

我试图在子程序中使用无限多态指针使用参数化派生类型。

是否可以对参数化类型使用“选择类型”子句?

我已经尝试了以下方法,但是遇到编译错误。
(TYPE或附近的语法错误)

module mod_real
  implicit none

  type :: type1(k)
    integer, kind :: k = 4
    real(kind=k) :: val
  end type type1

  contains

    subroutine out(in)
      class(*) :: in
      select type(in)
        type is (type1(4))
          print *, 'real(4):', in%val
        type is (type1(8))
          print *, 'real(8):', in%val
      end select
    end subroutine out

end module mod_real 

program real_test
  use mod_real

  type(type1(4)) :: p
  type(type1(8)) :: p2 

  p%val = 3.14
  p2%val = 3.1456d0

  call out(p)
  call out(p2)       

end program real_test 

带有“type is(type1(4))”和“type is(type1(8))”的行表示语法不正确。我正在使用Portland Group Fortran编译器(版本13.5-0)。

最佳答案

在撰写问题时,问题很可能是编译器支持,请检查此页面:

  • http://fortranwiki.org/fortran/show/Fortran+2003+status

  • 至于实际的问题,在这种情况下,您可以使用module procedure的编译时解决方案,该解决方案不需要多态性,因此可能会减少开销:
    module mod_real
    
    type type1(k)
      ... ! as before
    end type
    
    interface out
      module procedure out4, out8
    end interface
    
    contains
    
     subroutine out_type4(x)
       type(type1(4)), intent(in) :: x
       print*, 'real(4):' x%val   
     end subroutine
    
     subroutine out_type8(x)
       type(type1(8)), intent(in) :: x
       print*, 'real(8):' x%val   
     end subroutine
    
    end module
    program 
      ... ! as before
    end program
    

    关于types - Fortran: 'select type'子句中​​的参数化派生类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698561/

    相关文章:

    oop - 在 Julia 中重新定义类型 : invalid redefinition of constant

    Java 方法重写 - 使用参数时调用父方法

    fortran - 根据子程序参数创建常量

    iphone - iPhone 中的 Fortran 编辑器

    postgresql - Cast 产生 'Returned type character varying does not match expected type character varying(8)'

    c - 如何选择合适的 Haskell C 类型?

    数字类型的 C++ 非零默认值 - 重新发明?

    c++ - 用 CRTP + 可变参数模板模拟动态多态性

    c++ - 如何声明一个类显式抽象?

    fortran - 在 PURE 过程 Fortran 中调用类型绑定(bind)过程