在Fortran中何处放置“隐式无” [英] Where to put `implicit none` in Fortran

查看:199
本文介绍了在Fortran中何处放置“隐式无”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否需要在每个函数和子例程中放置隐式无



它在包含这些函数和子例程的模块的开头吗?



或者是足够把它放在使用这些模块的程序的开始处吗?



通过观察其他人的工作代码,所有这些地方都包含隐式无。我不确定是否这样做是多余的,因为从仍然编译的子例程中删除隐式无并产生相同的输出。



顺便说一下,我使用 gfortran fortran 90

解决方案

隐式语句(包括隐式无)适用于范围单元。这样的东西被定义为:BLOCK构造,派生类型定义,接口主体,程序单元或子程序,不包括所有嵌套的作用域单元在它中


这个排除其中的所有嵌套作用域单位表明可能有必要/希望拥有 implicit none 在模块中定义的每个函数和子例程(统称为过程)中。但是,模块中包含的内部过程存在基于 host 作用域单元的默认映射。因此,在模块中使用 implicit none 不需要在包含的过程中拥有它。



这个主机范围单位法则同样适用于内部程序。这意味着主程序中的隐式无包含其中包含的所有程序;这同样适用于模块程序的内部程序。块结构也可以看到这一点,并且隐含的语句甚至都不允许在其中之一中。



外部函数/子程序不会继承程序或模块的隐式行为,并且模块不会从使用它们的程序/其他模块继承它。这显然是有意义的,因为隐式类型在编译时必须知道,并且无论其最终用途如何,都应该明确定义。



另外,不能做

pre $隐含的
使用somemodule

结束程序

一个隐式语句必须遵循所有 use 语句。



这个主机范围单位规则值得注意的是,适用于接口机构。 IanH的回答激发了这种例外,但重新强调这件事足够重要。

 模块mod 
隐式无

接口
子程序external_sub()
!默认的隐式打字规则适用于这里,除非有一个隐含的
!声明,如隐含无。来自模块的那些在这里没有生效。
结束子程序
结束界面

结束模块

关于从子例程中去除隐式无的测试:如果代码对隐式无有效,那么它必须是没有该陈述的有效和相同。所有的实体必须在前一种情况下明确声明,所以后者不会应用隐式规则。


Do I need to put implicit none inside every function and subroutine?

Or is it enough to put it at the beginning of the module containing these functions and subroutines?

Or is it enough to put it at the beginning of the program that is using these modules?

From observation of other's working code, implicit none is included in all these places. I am not sure if this is done redundantly because removing implicit none from subroutines still compiled and produced the same output.

By the way, I am using gfortran fortran 90.

解决方案

The implicit statement (including implicit none) applies to a scoping unit. Such a thing is defined as

BLOCK construct, derived-type definition, interface body, program unit, or subprogram, excluding all nested scoping units in it

This "excluding all nested scoping units in it" suggests that it may be necessary/desirable to have implicit none in each function and subroutine (collectively, procedures) defined in a module. However, inside procedures contained within a module there is a default mapping based on the host scoping unit. So, with implicit none in the module it isn't necessary to have that in contained procedures.

This host scoping unit rule applies equally to internal programs. This means that implicit none in the main program covers all procedures contained in it; and that the same applies for internal programs of module procedures. Block constructs see this also, and the implicit statement isn't even allowed within one of these.

However, external functions/subroutines will not inherit implicit behaviour from a program or module, and modules don't inherit it from programs/other modules which use them. This clearly makes sense as the implicit typing must be known at compile time and be well defined regardless of their ultimate use.

Further, one cannot do

implicit none
use somemodule

end program

An implicit statement must follow all use statements.

This host scoping unit rule notably doesn't apply to interface bodies. IanH's answer motivates that exception, but it's an important enough thing to re-stress. It has caused much confusion.

module mod
 implicit none

  interface
    subroutine external_sub()
      ! The default implicit typing rules apply here unless there is an implicit
      ! statement, such as implicit none.  Those from the module aren't in force here.
    end subroutine
  end interface

end module

Regarding the test of removing implicit none from a subroutine: if the code is valid with implicit none then it must be valid and identical without that statement. All entities must be explicitly declared in the former case, so no implicit rules would be applied in the latter.

这篇关于在Fortran中何处放置“隐式无”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆