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

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

问题描述

良好的实践要求 Fortran 中的子例程参数每个都应具有指定的意图(即 intent(in)intent(out)intent(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
    ...

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

However, not specifying an intent is valid Fortran:

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

除了编译时检查指定为 intent(inout) 的参数和没有指定意图的参数之外,还有什么真正的区别吗?如果我将意图改造为旧的、无意图的代码,我应该担心什么?

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?

推荐答案

根据 Adams 等人的 The Fortran 2003 Handbook,intent(inout) 参数和参数之间存在一个区别没有明确的意图.intent(inout) 案例中的实际参数(即调用者中的参数)必须始终是可定义的.如果未指定意图,则参数必须是可定义的如果子例程的执行尝试定义虚拟参数.可定义 表示设置值:dummy_arg = 2.0.显然,如果这样做,实际参数应该是一个变量.对于意图(输入输出),无论子例程是否执行此操作,实际参数都必须是可定义的.如果没有指定意图,它取决于子例程的特定调用发生了什么——如果子例程没有定义变量,那没关系;如果是这样,那就有问题了——比如写入一个常量的实际参数显然会导致问题.

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