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

AlR*_*lRi 6 polymorphism fortran types parameterized

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

可以对参数化类型使用'select type'子句吗?

我已尝试过以下几行,但收到​​编译错误.(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 
Run Code Online (Sandbox Code Playgroud)

具有"type is(type1(4))"和"type is(type1(8))"的行被指示为具有不正确的语法.我使用的是Portland Group Fortran编译器(版本13.5-0).

Mar*_*kus 1

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

至于实际问题,在这种情况下,您可以使用编译时解决方案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
Run Code Online (Sandbox Code Playgroud)

  • 在撰写问题时,PGI 已声明完全符合 Fortran 2003。您没有回答是否可以在“选择类型”中使用参数化派生类型 (2认同)