Fortran过程指针指向不执行任何操作的子例程 [英] Fortran procedure pointer points to a subroutine that does nothing

查看:419
本文介绍了Fortran过程指针指向不执行任何操作的子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码

 
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)
enddo

The 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 subroutine

However, 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过程指针指向不执行任何操作的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆