Fortran意图(inout)与省略意图 [英] Fortran intent(inout) versus omitting intent

查看:144
本文介绍了Fortran意图(inout)与省略意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的做法表明Fortran中的子例程参数应该有指定的意图(即 intent(in) intent(out) intent(inout)):

 子程序栏(a,b)
真实,意图(中):: a
真实,意图(inout):: b
b = b + a
...

但是,不指定意图是有效的Fortran:

 











$ b $ ..

除编译时检查指定为 intent(inout)和一个没有指定意图的参数?有什么我应该担心的是,如果我正在改造意图更老,免费,代码?根据Adams等人的 Fortran 2003手册,有两种不同的没有指定意图的意图(inout)参数和参数。意图(inout)情况下的实际参数(即在调用者中)必须始终可定义。如果没有指定意图,则如果子例程的执行尝试定义伪参数,那么参数必须是可定义的 definable 表示设置值:dummy_arg = 2.0。显然,如果这样做了,实际的论点应该是一个变量。对于intent(inout),实际的参数必须是可定义的,不管子程序是否这样做。如果没有指定意图,它取决于该子例程的特定调用发生了什么 - 如果该子例程没有定义该变量,则它是OK的;如果它确实存在问题,那么写入一个常量的实际参数等情况显然会导致问题。

这并不意味着编译器会诊断所有这些情况 - 标准要求编译器进行诊断的是一个不同的问题。在编译时,几乎不可能检测到意图 - 未指定的情况要求的所有错误,因为违反取决于代码的运行时流程。编译器诊断意图(inout)的情况要容易得多,并警告你有关代码的问题。


Good practice dictates that subroutine arguments in Fortran should each have a specified intent (i.e. intent(in), intent(out) or intent(inout) as described this question):

subroutine bar (a, b)
    real, intent(in) :: a
    real, intent(inout) :: b
    b = b + a
    ...

However, not specifying an intent is valid Fortran:

subroutine bar (a, b)
    real, intent(in) :: a
    real :: b
    b = b + a
    ...

Are there any real differences beyond compile time checking for an argument specified as intent(inout) and an argument without a specified intent? Is there anything I should worry about if I'm retrofitting intents to older, intent free, code?

解决方案

According to The Fortran 2003 Handbook by Adams, et al., there is one difference between an intent(inout) argument and argument without specified intent. The actual argument (i.e., in the caller) in the intent(inout) case must always be definable. If the intent is not specified, the argument must be definable if execution of the subroutine attempts to define the dummy argument. definable means setting the value: dummy_arg = 2.0. Clearly the actual argument should be a variable if this is done. For intent(inout) the actual argument must be definable whether or not the subroutine does this. Without no intent specified, it depends on what happens on that particular invocation of the subroutine -- if the subroutine doesn't define the variable, it is OK; if it does, than there is a problem -- cases such as writing to an actual argument that is a constant will obviously cause problems.

This doesn't mean that the compiler will diagnose all of these cases -- what the standard requires a compiler to diagnose is a different issue. It would be close to impossible to detect all errors of the intent-not-specified case requirement at compile time, since violations depend on the run-time flow of the code. It is much easier for the compiler to diagnose the intent(inout) case and warn you of problems with the code.

这篇关于Fortran意图(inout)与省略意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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