如何判断一个fortran过程指针是否与特定的子程序相关联
How to judge if a fortran procedure pointer is associated with a specific subroutine
我想知道如何判断一个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
我知道关联函数可以用来判断一个过程指针是否关联,但是我怎么知道关联的是哪个子程序呢?对于这里,if bc associated with boundaryA or boundaryB?
我试过了
associated(a%bc, boundaryA)
编译器 (gfortran 4.8.2) 给出了一个错误,即 'target' argument of 'associated' intrinsic at boundaryA must be the same type and kind as 'pointer'.
如果删除 nopass 属性,编译器会给出一个错误,即 'bc' 的参数 'i' with pass(i) at 'bc' must be of derived type '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
我想知道如何判断一个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
我知道关联函数可以用来判断一个过程指针是否关联,但是我怎么知道关联的是哪个子程序呢?对于这里,if bc associated with boundaryA or boundaryB?
我试过了
associated(a%bc, boundaryA)
编译器 (gfortran 4.8.2) 给出了一个错误,即 'target' argument of 'associated' intrinsic at boundaryA must be the same type and kind as 'pointer'.
如果删除 nopass 属性,编译器会给出一个错误,即 'bc' 的参数 'i' with pass(i) at 'bc' must be of derived type '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