在 Gfortran 中使用可选参数时接口不明确
Ambiguous interface when using optional arguments in Gfortran
我在编译一段代码时偶然发现了这个错误,我已经使用了很长时间没有出现问题。我在 Linux 上使用 Gfortran 8.2,我怀疑编译器更新导致了这个问题。
当我定义一个带有可选参数的接口时,该接口具有不同数量的非可选参数,Gfortran 会抱怨该接口不明确。例如,如果我编译以下内容,我会得到 "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
的数据具有不同的等级,那么它编译得很好。如果我向两个函数添加另一个非可选参数,我会收到相同的错误消息。
我偶然发现的实际代码是 this file 中的 Result
接口,正如我所说,它曾经按预期编译。
感谢任何帮助!
Gfortran complains that the interface is ambiguous
嗯,那是因为接口不明确。
下面的调用应该选择哪个过程?
integer :: param
print *, Test(param)
test1
参数 opt
选择加入?或者...
test2
参数 data
已通过且 opt
选择退出?
如果它只是在更新后才开始失败,那可能是一个非常受欢迎的错误修复。
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.
同样的问题。如果这两个函数都有一个额外的非可选参数并且符合类型-种类-等级,您将如何解决调用?
假设新参数是 data_extra
:
print*, Test(param1, param2)
test1
参数 data_extra
已通过且 opt
已选择加入?或者...
test2
参数 data
和 data_extra
通过并 opt
选择退出?
我在编译一段代码时偶然发现了这个错误,我已经使用了很长时间没有出现问题。我在 Linux 上使用 Gfortran 8.2,我怀疑编译器更新导致了这个问题。
当我定义一个带有可选参数的接口时,该接口具有不同数量的非可选参数,Gfortran 会抱怨该接口不明确。例如,如果我编译以下内容,我会得到 "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
的数据具有不同的等级,那么它编译得很好。如果我向两个函数添加另一个非可选参数,我会收到相同的错误消息。
我偶然发现的实际代码是 this file 中的 Result
接口,正如我所说,它曾经按预期编译。
感谢任何帮助!
Gfortran complains that the interface is ambiguous
嗯,那是因为接口不明确。 下面的调用应该选择哪个过程?
integer :: param
print *, Test(param)
test1
参数opt
选择加入?或者...test2
参数data
已通过且opt
选择退出?
如果它只是在更新后才开始失败,那可能是一个非常受欢迎的错误修复。
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.
同样的问题。如果这两个函数都有一个额外的非可选参数并且符合类型-种类-等级,您将如何解决调用?
假设新参数是 data_extra
:
print*, Test(param1, param2)
test1
参数data_extra
已通过且opt
已选择加入?或者...test2
参数data
和data_extra
通过并opt
选择退出?