我有一个接口块来定义一个通用子例程,它的假设大小数组作为虚拟参数(以便能够对传递的数组的‘中间’进行操作,就像一个C指针),并且它不编译。下面是简单的例子:
module foo
interface sub
module procedure isub
module procedure dsub
end interface
contains
subroutine isub(a,n)
integer, intent(in) :: a(*), n
integer :: i
print*, 'isub'
do i=1,n
print*, a(i)
enddo
end subroutine isub
subroutine dsub(a)
real(8), intent(in) :: a(*)
integer, intent(in) :: n
integer :: i
print*, 'dsub'
do i=1,n
print*, a(i)
enddo
end subroutine dsub
end module foo
program test
use foo
implicit none
integer :: ai(4)
real(8) :: ad(4)
ai=(/1,2,3,4/)
ad=(/1.,2.,3.,4./)
call sub(ai,3)
call sub(ad,3)
call isub(ai(2),3)
!call sub(ai(2),3)
end program test注释行不编译,但是当直接使用call isub(ai(2),3) (用gfortran和ifort测试)调用子程序时,它是可以的。为什么和有可能让它与call sub(ai(2),3)一起工作?
编辑:使用ifort的,它说:
$ ifort overload.f90
overload.f90(37): error #6285: There is no matching specific subroutine for this generic subroutine call. [SUB]
call sub(ai(2),3)
-------^
compilation aborted for overload.f90 (code 1)谢谢
发布于 2014-06-18 14:59:08
您正在将标量传递给期望数组的函数。试一试
call sub(ai(2:2))它正在传递一个长度为1的数组。我想知道为什么call isub(ai(2))会被接受.
回答你的新问题(部分在评论中):
如果将自己限制在连续数组上,则可以使用call sub(ai(2:4)) ,而不需要使用延迟形状数组来降低性能:
subroutine isub(a)
integer,intent(in) :: a(:)
integer :: i
print*, 'isub'
do i=1,size(a)
print*, a(i)
enddo
end subroutine isub没有用ifort或gfortran创建的临时数组。您可以使用以下方法进行检查:
ifort -check arg_temp_createdgfortran -Warray-temporarieshttps://stackoverflow.com/questions/24287436
复制相似问题