在 Fortran 中将“隐式无"放在哪里 [英] Where to put `implicit none` in Fortran

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

问题描述

我需要把 implicit none 放在每个函数和子程序中吗?

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?

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

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.

顺便说一句,我正在使用 gfortran fortran 90.

By the way, I am using gfortran fortran 90.

推荐答案

implicit 语句(包括 implicit none)适用于作用域单元.这样的事情被定义为

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

BLOCK 构造、派生类型定义、接口主体、程序单元或子程序,不包括其中的所有嵌套范围单元

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

这不包括其中的所有嵌套范围单元";建议在模块中定义的每个函数和子例程(统称为过程)中包含 implicit none 可能是必要的/可取的.但是,在模块中包含的过程内部有一个基于 host 作用域单元的默认映射.因此,如果在模块中使用 implicit none,则不需要在包含的过程中使用它.

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.

此主机范围单元规则同样适用于内部程序.这意味着主程序中的implicit none涵盖了其中包含的所有程序;这同样适用于模块过程的内部程序.块构造也看到了这一点,并且 implicit 语句甚至不允许在其中之一中使用.

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 apply implicit rules from one program unit to a module it uses, such as in:

implicit none
use somemodule

end program

implicit 语句必须跟在所有 use 语句之后.

An implicit statement must follow all use statements.

同样,子模块本身就是一个程序单元,与它的祖先不同.模块或子模块是扩展它的子模块的,而不是主机:主机范围单元规则不适用,子模块不继承从其父映射规则.如果子模块范围内没有 implicit 语句,则默认规则将在那里应用.

Equally, a submodule is a program unit in itself, distinct from its ancestors. A module or submodule is a parent, not a host, of a submodule which extends it: the host scoping unit rule doesn't apply and the submodule doesn't inherit the mapping rules from its parent. Without an implicit statement in the submodule's scope the default rules will apply there.

主机范围单元规则尤其不适用于接口主体.IanH 的回答 激发了这个例外,但这是一件足以重新强调的重要事情.这引起了很多混乱.

The 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

关于从子例程中删除 implicit none 的测试:如果代码在 implicit none 下有效,那么在没有该语句的情况下它必须是有效且相同的.在前一种情况下,所有实体都必须显式声明,因此在后一种情况下不会应用隐式规则.

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天全站免登陆