Fortran:在“select type”子句中使用带参数派生类型

7

我正在尝试在子程序中使用带参数派生类型,使用无限多态指针。

是否可以使用“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 

"type is (type1(4))" 和 "type is (type1(8))" 这两行被标为语法错误。我使用的是Portland Group Fortran编译器(版本13.5-0)。


看起来不错,你使用的是哪个版本的PGI Fortran? - steabert
我正在使用版本13.5-0。 - AlRi
2
注意,大多数编译器尚未完全实现参数化派生类型。也许你的编译器只有部分支持。 - Bálint Aradi
1个回答

1

在提问时,问题最有可能是与编译器支持相关的,请查阅此页面:

关于实际问题,您可以使用模块过程的编译时解决方案,在此情况下不需要多态性,因此可能会有更少的开销。
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

2
在撰写此问题时,PGI已经声称完全符合Fortran 2003标准。您没有回答是否可以在“select type”中使用参数化派生类型。 - Vladimir F Героям слава

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接