首页 文章

我是否需要一个显式接口来在子例程中分配派生类型的组件?

提问于
浏览
0

我有一个派生类型:

module foo
  type bar
      integer, allocatable, dimension(:) :: data
  end type bar
end module foo

现在我想在没有显式接口的子程序中分配 bar 的数据:

program main
  use foo
  type(bar) :: mybar
  call alloc_my_bar(10,mybar)
  print*, mybar
end program

subroutine alloc_my_bar(n,mybar)
  use foo
  type(bar) :: mybar
  integer :: n
  allocate(mybar%data(n))
  mybar%data = 42
end subroutine alloc_my_bar

这似乎与 ifort 一样正常,但我知道如果mybar不是用户定义类型的一部分,我需要一个显式接口...将可分配数组放入用户定义类型中是否需要显式接口?该代码与(F90,F95,F2003 ......)兼容的fortran标准的版本是什么?

1 回答

  • 2

    可分配组件在TR15581至F95中定义,该组件已纳入Fortran 2003标准 . 你不应该需要显式接口,只需要使用类型定义的关联就可以了 . 您没有传递数组,而是传递它周围的结构 .

相关问题