在PURE过程Fortran中调用类型绑定过程

问题描述 投票:3回答:1

如果我已声明类型

 type test(NSIZE)
  integer, len :: NSIZE
  real :: dummy(NSIZE)
  contains 
  procedure, pass(this) :: proc 

  end test
  type(test(NSIZE=10)) :: test_type

我的proc子程序是PURE。我的proc返回说一个值,没有任何副作用。

 pure subroutine proc(this, n) 
  implicit none 
 class(test(NSIZE=*)), intent(inout) :: this 
 integer, intent(inout) :: n
    n = n +1 
 end subroutine proc

现在在另一个子例程中也声明为PURE我调用proc

  pure subroutine test2 

    integer :: n 

     call  test_type% proc(n)
  end subroutine test2 

我得到call test% proc(n)上的错误说:

error #7140: This global use associated object appears in a 'defining' context in a PURE procedure or in an internal procedure contained in a PURE procedure.

一个自包含的例子

module mod1
   implicit none

      type test (size)
         integer, len :: size
         real :: dum(size)
      contains
         procedure, pass(this) :: dum_proc
      end type

      type(test(size=10)) :: test1

   contains

      pure subroutine dum_proc(this,  n )
         implicit none
         class(test(size=*)), intent(inout) :: this
         integer, intent(out) :: n
         n =n +2
      end subroutine dum_proc
end module mod1


program SUPPORT


implicit none
integer :: n

n = 0

call caller(n)


contains
   pure subroutine caller( nk )
   use mod1, only : test1
   implicit none

   integer, intent(inout) :: nk

   call test1% dum_proc(nk)

   end subroutine

end program SUPPORT`
fortran intel-fortran
1个回答
2
投票

你的问题来自通话

call test1% dum_proc(nk)

因为纯子例程test1中的caller已被使用关联,所以不允许它是与intent(inout)属性的伪参数对应的实际参数。在对类型绑定过程的调用中,test1与传递对象伪参数this(具有该意图)相关联。

intent(inout)伪参数相关联被视为变量定义上下文,并且正是这种“定义'上下文”在错误消息中表示。实际上,为了使它处于定义的上下文中,实际上没有必要进行更改。

相反,如果您将test1作为intent(inout)伪参数,则此限制不适用。将test1主机关联具有相同的限制,就像使用关联一样。

© www.soinside.com 2019 - 2024. All rights reserved.