我想知道如何判断一个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处“关联”内在函数的“目标”参数必须与“指针”具有相同的类型和种类。
如果删除 nopass 属性,编译器会给出错误,即“bc”处的参数“i”和“bc”处的 pass(i) 必须是派生类型“bc_type”。
最佳答案
这是一种方法。
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
关于pointers - 如何判断Fortran过程指针是否与特定子程序关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48972233/