内在多态赋值是一些 Fortran 编译器(例如 ifort 18、nagfor 6.2)的最新功能,在旧版本(例如 ifort 17、gfortran 6.3)中不可用。与这些旧版本一起使用的众所周知的解决方案是使用如下示例中定义的分配(取自 Chivers 和 Sleightholme 的书并改编自):
module deftypes
type, abstract :: shape_t
integer :: x = 0, y = 0
end type shape_t
type, extends(shape_t) :: circle_t
integer :: radius = 0
end type circle_t
interface assignment(=)
module procedure generic_shape_assign
end interface
contains
subroutine generic_shape_assign ( lhs, rhs )
class(shape_t), intent(in ) :: rhs
class(shape_t), allocatable, intent(out) :: lhs
print*,' --> in generic_shape_assign'
allocate(lhs, source = rhs)
end subroutine generic_shape_assign
end module deftypes
program check_assign
use deftypes
implicit none
class(shape_t), allocatable :: myshape
type (circle_t) :: mycirc1, mycirc2
mycirc1 = circle_t ( 1, 2, 3 )
print*,'A polymorphic assignment: myshape = mycirc1'
myshape = mycirc1
print*,'An intrinsic assignment: mycirc2 = mycirc1'
mycirc2 = mycirc1
end program check_assign
此示例可与 ifort 15.0.3 和 gfortran 6.3.0 一起编译并运行良好。但是对于 nagfor 6.2,我在编译期间收到以下错误(对于 line mycirc2=mycirc1
):
Error: check_assign.f90, line 41: Incorrect data type CIRCLE_T (expected SHAPE_T) for argument LHS (no. 1) of GENERIC_SHAPE_ASSIGN
我不清楚为什么这个编译器试图在指令中使用定义的赋值,mycirc2 = mycirc1
而这两个变量不是可分配的多态变量。
当然,如果我删除定义的赋值,它适用于 nagfor 但不适用于其他旧编译器。知道这个错误来自哪里以及如何解决它吗?