在Gfortran中使用可选参数时的接口不明确 [英] Ambiguous interface when using optional arguments in 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.
我偶然发现的实际代码是
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)
-
test1
是否带有参数opt
?或者... -
test2
,其中传递了参数data
,并且选择了opt
?
test1
with the argumentopt
opted-in? Or...test2
with the argumentdata
passed andopt
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)
-
test1
传递了参数data_extra
并选择加入了opt
?或者... -
test2
传递了参数data
和data_extra
并选择退出了opt
?
test1
with the argumentdata_extra
passed andopt
opted-in? Or...test2
with the argumentsdata
anddata_extra
passed andopt
opted-out?
这篇关于在Gfortran中使用可选参数时的接口不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!