问题
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.
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
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.
Any help appreciated!
回答1:
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
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.
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.
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
with the argumentdata_extra
passed andopt
opted-in? Or...test2
with the argumentsdata
anddata_extra
passed andopt
opted-out?
来源:https://stackoverflow.com/questions/53724340/ambiguous-interface-when-using-optional-arguments-in-gfortran