Fortran过程指针指向不执行任何操作的子例程

编程入门 行业动态 更新时间:2024-10-26 07:32:19
本文介绍了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过程指针指向不执行任何操作的子例程

本文发布于:2023-07-22 04:05:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   例程   过程   操作   Fortran

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!