我有这样的代码
: procedure(),pointer :: p if()p => job1 else p => job2 endif do i = 1,a_big_number call x(...) call p(i,j,k) enddo子程序'job1'做了一些工作,但子程序'job2'没有任何作用。换句话说,在某些情况下,我需要完成'x'和'job1'。在其他情况下,我只需要做'x'。我的问题是我应该如何定义job2。它似乎简单地使用null()不起作用。我现在正在做的是: $ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' in):: i,j,k end subroutine
然而,这看起来很愚蠢,编译代码时我收到了很多编译警告,因为没有使用这些参数。有没有更聪明的方法来做到这一点?
解决方案在没有任何用处的情况下,您可以取消程序指针,然后在通过指针调用过程之前测试关联状态。
PROCEDURE(interface_that_matches_job1),POINTER :: p IF(...)THEN p => job1 ELSE NULLIFY(p)!或者p => NULL() END IF DO i = 1,a_big_number CALL x(...) IF(ASSOCIATED(p))CALL p(i, j,k) END DO
I have a code like this
: procedure(),pointer :: p if () p => job1 else p => job2 endif do i=1,a_big_number call x(...) call p(i,j,k) enddoThe subroutine 'job1' does some work, but the subroutine 'job2' does nothing. In other words, under some circumstances, I need to finish 'x' and 'job1'. Under other circumstances, I only need to do 'x'. My question is how should I define job2. It seems simply using null() does not work. What I am doing right now is like:
subroutine job2(i,j,k) integer,intent(in) :: i,j,k end subroutineHowever, this looks silly, and I got a lot compiling warning when I compiled the code because those arguments were not used. Is there a smarter way to do this?
解决方案You could nullify the procedure pointer in the case that there was nothing useful to do, and then test the association status prior to invoking the procedure through the pointer.
PROCEDURE(interface_that_matches_job1), POINTER :: p IF (...) THEN p => job1 ELSE NULLIFY(p) ! Or p => NULL() END IF DO i = 1, a_big_number CALL x(...) IF (ASSOCIATED(p)) CALL p(i,j,k) END DO
更多推荐
Fortran过程指针指向不执行任何操作的子例程
发布评论