Fortran 多态性可以用于不同级别的可分配数组吗?

问题描述 投票:0回答:1

Fortran 子例程可以使用多态性来做这样的事情吗:

subroutine (inarr)

class(*) :: inarr

select type(inarr)

type is (real, allocatable(:))
   ! do something for 1-D array, maybe even allocate it
type is (real, allocatable(:,:))
   ! do something for 2-D array, maybe even allocate it
type is etc.

上面的语法当然行不通。但我想我想做的事情很明确。有什么办法可以做到吗?

fortran polymorphism
1个回答
0
投票

也许你想要的是假定的排名特征:

subroutine (inarr)

real, allocatabe :: inarr(..)   ! .. denote any possible rank

select rank(inarr)

   rank(1)
      ! do something for 1-D array, maybe even allocate it
      allocate( arr(n) )
      ! ...
   rank(2)
      ! do something for 2-D array, maybe even allocate it
      allocate( arr(n,m) )
      ! ...

end select

© www.soinside.com 2019 - 2024. All rights reserved.