在Gfortran中使用可选参数时的接口不明确 [英] Ambiguous interface when using optional arguments in Gfortran

查看:73
本文介绍了在Gfortran中使用可选参数时的接口不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译一些我已经使用了很长时间的代码时,我偶然发现了这个错误.我在Linux上使用Gfortran 8.2,我怀疑编译器更新导致了此问题.

I've just stumbled across this error when compiling a bit of code that I've been using without problems for ages now. I'm using Gfortran 8.2 on Linux and I suspect that a compiler update has caused the issue.

当我定义一个带有可选参数的接口时,该可选参数具有不同数量的非可选参数,Gfortran抱怨该接口是模棱两可的.例如,如果我编译以下内容,则会得到通用接口'test'中的歧义接口(1)中的'testinit1'和(2)中的'testinit2'):

When I define an interface with an optional argument that has a different number of non-optional arguments, Gfortran complains that the interface is ambiguous. For example, if I compile the following, I get "Ambiguous interfaces in generic interface 'test' for ‘testinit1’ at (1) and ‘testinit2’ at (2)":

module test_mod
implicit none

interface Test
    module procedure test1, test2
end interface

contains

function test1(opt) result(rslt)
    integer :: rslt
    integer, optional :: opt
    rslt = 1
end function

function test2(data, opt) result(rslt)
    integer :: rslt
    integer :: data
    integer, optional :: opt
    rslt = data
end function

end module

如果我删除可选参数 opt ,那么它可以正常编译.如果我向 test1 添加一个 data 参数,并且该参数与 test2 的数据具有不同的等级,则它可以很好地编译.如果我在两个函数中都添加了另一个非可选参数,则会得到相同的错误消息.

If I remove the optional argument opt, then it compiles fine. If I add a data argument to test1 that has a different rank to test2's data, then it compiles fine. If I add another non-optional argument to both functions, I get the same error message.

我偶然发现的实际代码是Result 接口.f90"rel =" nofollow noreferrer>此文件,正如我所说的,该文件过去曾按预期进行编译.

The actual code I stumbled across in is the Result interface in this file, which, as I say, used to compile as expected.

任何帮助表示赞赏!

推荐答案

Gfortran抱怨界面不明确

Gfortran complains that the interface is ambiguous

好吧,这是因为界面 含糊不清.在接下来的调用中应该选择哪个过程?

Well, that's because the interface is ambiguous. Which procedure should be chosen in the following call?

integer :: param
print *, Test(param)

  1. test1 是否带有参数 opt ?或者...
  2. test2 ,其中传递了参数 data ,并且选择了 opt ?
  1. test1 with the argument opt opted-in? Or...
  2. test2 with the argument data passed and opt opted-out?

如果仅在更新后才开始失败,则可能是非常受欢迎的错误修复程序.

If it started to fail only after an update, that was probably a very welcome bug-fix.

如果我删除可选参数opt,那么它可以正常编译.如果我向test1添加一个数据参数,该参数与test2的数据具有不同的等级,那么它可以很好地编译.

If I remove the optional argument opt, then it compiles fine. If I add a data argument to test1 that has a different rank to test2's data, then it compiles fine.

说得通.没有可选参数,这两个函数在参数数量上是完全明确的.改变论据的排名也会引起差异.

Makes sense. Without the optional argument, both functions are completely unambiguous by number of arguments. Changing the rank of the argument also cause differentiation.

如果我在两个函数中都添加了另一个非可选参数,则会收到相同的错误消息.

If I add another non-optional argument to both functions, I get the same error message.

相同的问题.如果两个函数都有一个额外的非可选参数,且类型一致,则如何解决该调用?假设新参数是 data_extra :

Same problem. If both functions had an extra non-optional argument with type-kind-rank conformance, how would you resolve the call? Suppose the new argument is data_extra:

print*, Test(param1, param2)

  1. test1 传递了参数 data_extra 并选择加入了 opt ?或者...
  2. test2 传递了参数 data data_extra 并选择退出了 opt ?
  1. test1 with the argument data_extra passed and opt opted-in? Or...
  2. test2 with the arguments data and data_extra passed and opt opted-out?

这篇关于在Gfortran中使用可选参数时的接口不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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