0

我有两个例程,它们的区别仅在于参数的顺序,但我想通过接口使用它们,以便在调用时根据参数的顺序调用正确的例程。编译器抱怨它无法区分它们,我的猜测是因为如果我在调用时使用命名参数的语法,它将不知道要调用哪个。一种解决方法是为参数使用不同的名称,但我想知道是否有一种方法可以禁用命名参数调用样式。

例如,这是我要处理的情况

module Foo

    interface Bar
        module procedure Bar1
        module procedure Bar2
    end interface

contains
    subroutine Bar1(i,r)
        integer, intent(in) :: i
        real, intent(in) :: r
        print *, "bar1"
    end subroutine
    subroutine Bar2(r,i)
        real, intent(in) :: r
        integer, intent(in) :: i
        print *, "bar2"
    end subroutine

end module

program fuux
    use Foo
    integer :: i
    real :: r

    r = 5.0
    i = 3

    call Bar(i,r) ! note that if I call Bar(i=i, r=r) the compiler cannot disambiguate
                  ! so it will complain at the interface statement
end program
4

1 回答 1

1

我不知道有什么方法可以按照您的建议去做,或者更确切地说是按照我认为您在“禁用命名参数调用样式”这句话中所建议的去做。如果这个答案以任何方式激怒、失望或不快,请发布一些代码,我们(所以我的意思是,我没有太多的架子和优雅,很少使用皇室的“我们”)可能会提出一个狡猾的把戏,这会取悦你。

编辑

没有直接的方法可以做你想做的事。我想到的第一个解决方法是定义一个名为的子例程bar,它以规范的顺序获取一系列参数,并以适当的顺序简单地调用bar1, (以及您想要定义的bar2任何其他婴儿)。bars

于 2012-04-19T08:52:01.213 回答