Ambiguous interface when using optional arguments in Gfortran

白昼怎懂夜的黑 提交于 2021-02-05 06:03:31

问题


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)
  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.

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)
  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?


来源:https://stackoverflow.com/questions/53724340/ambiguous-interface-when-using-optional-arguments-in-gfortran

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!