首页 文章

如何通过省略intent的子例程强制fortran编译器生成错误“intent(in)”

提问于
浏览
3

这个问题与我之前的问题有关:How to force compiler to interpret omitted intent as intent(inout) . 似乎不可能将省略的意图解释为意图(inout),因此违反意图(in)的问题仍然存在 .

同样的例子:

module test
  implicit none
  contains

  subroutine fun1(x)
    real(8), intent(in)::x
    call fun2(x)               
  end subroutine

  subroutine fun2(x)
   real(8) :: x
   x = 10
  end subroutine
end module

可以编译此代码,而不会出现gfortran和ifort的任何错误/警告 . 所以我的问题是:

当intent(in)变量被传递给具有省略intent的子例程(但是使用声明的接口)时,如何强制fortran编译器生成错误?

1 回答

  • 2

    正如IanH所说,你需要一个可以为你选择的处理器(即编译器) . 例如,如果你给它正确的标志,那么NAG编译器会做(免责声明 - 我为NAG工作) . 我修改了你的代码以使其可移植并添加了一个驱动程序来显示:

    $ cat t.f90 
    module test
      implicit none
    
      Integer, Parameter :: wp = Selected_real_kind( 12, 70 )
    
      contains
    
      subroutine fun1(x)
        real(wp), intent(in)::x
        call fun2(x)               
      end subroutine
    
      subroutine fun2(x)
       real(wp) :: x
       x = 10
      end subroutine
    end module
    
    Program test_test
    
      Use test
    
      Implicit None
    
      Real( wp ) :: x
    
      x = 5.0_wp
    
      Call fun1( x ) 
    
    End Program test_test
    $ nagfor t.f90
    NAG Fortran Compiler Release 5.3.1 pre-release(904)
    [NAG Fortran Compiler normal termination]
    $ ./a.out
    $ nagfor -C=all -C=undefined t.f90 
    NAG Fortran Compiler Release 5.3.1 pre-release(904)
    [NAG Fortran Compiler normal termination]
    $ ./a.out
    Runtime Error: t.f90, line 15: Dummy argument X is associated with an expression - cannot assign
    Program terminated by fatal error
    Aborted (core dumped)
    $
    

    所以搜索旗帜,可能会有所帮助 - 如果没有抱怨任何提供编译器的人!

相关问题