Fortran 包装函数中的语句标签
在Fortran中,有没有一种方法可以通过包装函数传递语句标签 为了详细说明,我正在为Fortran 包装函数中的语句标签,fortran,Fortran,在Fortran中,有没有一种方法可以通过包装函数传递语句标签 为了详细说明,我正在为open()编写一个包装器,如下所示: program test contains subroutine
open()
编写一个包装器,如下所示:
program test
contains
subroutine my_open(unit, file, err)
integer, intent(in) :: unit
character(*), intent(in) :: file
integer, intent(in), optional :: err
if (present(err)) then
open(unit, FILE=file, ERR=err)
else
open(unit, FILE=file)
end if
end subroutine my_open
end program test
(当然,我的实际程序包含更多逻辑…)。但是,gfortran
抱怨
open(unit, FILE=file, ERR=err)
1
Error: Invalid value for ERR specification at (1)
有没有办法做到这一点?与
err
对应的标签必须是打开的范围单元中的标签。所以你想做的是不可能的。另外,为了回答更一般的问题,将标签作为变量传递本身是不可能的(至少,因为Fortran 95删除了指定的格式和GOTO)
但是,对于特定情况,可以使用iostat
,并将控制权传递回调用子程序
类似于(简化的):
可以说,这比将本质上是goto
的内容与子例程中的代码部分完全不同要好得多。[err
在最好的时候并不流行。]此外,这种状态传递可以推广到您希望传递标签的其他情况
call my_open(12, file, status)
! Instead of:
! 10 STOP
if (status.ne.0) then
!...
end if
end
subroutine my_open(unit, file, status)
integer, intent(in) :: unit
character(*), intent(in) ::file
integer, intent(out) :: status
open(unit, file=file, iostat=status)
if (iostat.ne.0) return
end subroutine