Function 函数结果中的Fortran类(*)

Function 函数结果中的Fortran类(*),function,class,return,fortran,Function,Class,Return,Fortran,我在这篇文章中详细介绍的函数中遇到了一个错误 出现此问题是因为我试图返回与 输入类型。有人能提出解决办法吗?我最初为每一个都有一个函数 键入,然后键入一个泛型接口以将它们分组到相同的名称中。现在我正在努力 使用多态性将所有内容放在单个函数中 这是gfortran给我的错误 gfortran -o build/lib/foul.o -c -ffree-form -g -J./build/lib lib/foul.f lib/foul.f:471.45: Function cassign (exp

我在这篇文章中详细介绍的函数中遇到了一个错误

出现此问题是因为我试图返回与 输入类型。有人能提出解决办法吗?我最初为每一个都有一个函数 键入,然后键入一个泛型接口以将它们分组到相同的名称中。现在我正在努力 使用多态性将所有内容放在单个函数中

这是gfortran给我的错误

gfortran -o build/lib/foul.o -c -ffree-form -g -J./build/lib lib/foul.f
lib/foul.f:471.45:

Function cassign (expr, a, b, wrn) Result (c)
我尝试使用可分配数组。在主程序中,我执行以下操作

Character (len=65), Allocatable :: sc(:)
Integer, Allocatable :: ic(:)
Real, Allocatable :: rc(:)

Allocate (sc(1))
Allocate (ic(1))
Allocate (rc(1))

sc = cassign (ra < rb, sa, sb)
ic = cassign (ra < rb, ia, ib)
rc = cassign (ra < rb, ra, rb)

我不确定我是否建议做我在下面写的事情,宁愿保留泛型,但我会尝试解释

首先要注意的是,正如错误消息所述,对于非伪参数多态变量(如
c
),该变量必须具有
指针
可分配
属性。在这里,函数结果是可分配的是有意义的

添加
allocatable
属性后,您似乎经历了两件与可分配多态变量赋值相关的事情:一次在函数中设置结果,一次使用函数的结果

您正在使用的gfortran版本(显然)不支持对多态变量的内在赋值。你可以使用等价物,可以说它的意图更加明确:

allocate (c, source=a)  ! You may also need to provide bounds for c
                        ! for some gfortran.
这是函数中赋值问题的解决方案

但是,对于函数result,现在返回的是多态结果。这意味着接受赋值的变量也必须是多态的,或者赋值不能是内在的。这是

错误:无法在(1)处将类(*)转换为整数(4)

尝试内部赋值时出错

要么使一切都多态,坚持使用泛型,要么使用定义的赋值。后一种情况的简化示例如下。[根据需要进行调整和扩展。]

module hello_bob
  interface assignment(=)
    module procedure int_equal_func_class
  end interface

contains

  subroutine int_equal_func_class(a,b)
    integer, intent(out) :: a(:)
    class(*), intent(in) :: b(:)

    select type (b)
      type is (integer)
        a = b
    end select
  end subroutine int_equal_func_class

   function func(a)
     class(*), intent(in) :: a(:)
     class(*), allocatable :: func(:)

     ! No intrinsic assignment supported, also see note about bounds
     allocate(func, source=a)
   end function func

end module hello_bob

program bob
  use hello_bob
  integer i(4)

  i=func([1,2,3,4])
  print*, i

end program bob

您是否尝试过使
c
可分配(如错误消息所示)?或者你想让它不出现,这样你就可以用有趣的方式使用结果了?我使用的是gfortran 4.9,这是最新的。
lib/fould.f:497.10:c=a1错误:对(1)处的可分配多态变量的赋值还不受支持
似乎有效。但是我必须返回一个数组,而不仅仅是一个变量。
gfortran-o build/utests/test\u foul.o-c-ffree form-g-J./build/lib-utests/test\u foul.f:315.7:sc=cassign(ra
在这个新的多态思想在我脑海中形成之前,我就在使用泛型。错误:在(1)处分配给可分配多态变量还不受支持。我已经走在时代的前面,应该坚持使用泛型。另外,我不想在代码中使用变通方法。如果当前不支持,我将不使用它。否则,它会造成一些不言而喻的复杂情况。此外,一旦该功能得到支持,旧代码就不应该再存在,而应该只拥有分配给多态变量的代码。我同意错误的解决方法,但在这种情况下,这并不太糟糕(请参阅内部注释)。另外,添加定义的赋值和做泛型一样多,所以我真的建议使用后者。不过,选择是好的。
allocate (c, source=a)  ! You may also need to provide bounds for c
                        ! for some gfortran.
module hello_bob
  interface assignment(=)
    module procedure int_equal_func_class
  end interface

contains

  subroutine int_equal_func_class(a,b)
    integer, intent(out) :: a(:)
    class(*), intent(in) :: b(:)

    select type (b)
      type is (integer)
        a = b
    end select
  end subroutine int_equal_func_class

   function func(a)
     class(*), intent(in) :: a(:)
     class(*), allocatable :: func(:)

     ! No intrinsic assignment supported, also see note about bounds
     allocate(func, source=a)
   end function func

end module hello_bob

program bob
  use hello_bob
  integer i(4)

  i=func([1,2,3,4])
  print*, i

end program bob