我想知道如何判断fortran过程指针是否与特定子例程相关联。以下是MWE(基本上基于my previous question)
# module file
module boundary
implicit none
type bc_type
procedure(boundary_type), pointer, nopass :: bc
! some other type-bound parameters
end type
abstract interface
subroutine boundary_type(i)
integer :: i
end subroutine
end interface
contains
subroutine boundaryA(i)
integer :: i
print*, 'Boundary A at ',i
end subroutine
subroutine boundaryB(i)
integer :: i
print*, 'Boundary B at ',i
end subroutine
end module
# main file
program main
use boundary
implicit none
type(bc_type) :: a
a%bc => boundaryA
end program
我知道关联的函数可用于判断过程指针是否关联,但我怎么知道它与哪个子例程相关联?对于这里,如果bc与boundaryA或boundaryB?
相关联我试过
associated(a%bc, boundaryA)
编译器(gfortran 4.8.2)给出一个错误,即在boundaryA的'associated'内在的'target'参数必须与'pointer'的类型和种类相同。
如果删除了nopass属性,编译器会给出一个错误,即“bc”的参数'i'与'bc'的pass(i)必须是派生类型'bc_type'。
答案 0 :(得分:1)
这是一种方法。
program main
use boundary
use, intrinsic :: iso_c_binding
implicit none
type(bc_type) :: a
a%bc => boundaryA
if (same_proc(c_funloc(a%bc),c_funloc(boundaryA))) print *, "boundaryA"
if (same_proc(c_funloc(a%bc),c_funloc(boundaryB))) print *, "boundaryB"
contains
function same_proc (a,b)
use, intrinsic :: iso_c_binding
logical same_proc
type(c_funptr), intent(in) :: a,b
same_proc = transfer(a,0_C_INTPTR_T) == transfer(b,0_C_INTPTR_T)
end function same_proc
end program