Bál*_*adi 7 fortran fortran95 fortran2003
我有一个wrapper包含其他派生类型(over)的派生类型().对于后者,赋值运算符已经过载.由于派生类型的赋值按默认的组件方式发生,我希望分配两个实例wrapper会over在某个时刻调用重载的赋值.但是,使用下面的程序似乎并非如此.仅当我还重载用于在实例之间wrapper包含显式赋值的赋值时over(通过取消注释注释的代码行),才会调用重载的赋值.为什么?我发现它有点违反直觉.有没有办法避免包装类型的过载?
module test_module
implicit none
type :: over
integer :: ii = 0
end type over
type :: wrapper
type(over) :: myover
end type wrapper
interface assignment(=)
module procedure over_assign
!module procedure wrapper_assign
end interface assignment(=)
contains
subroutine over_assign(other, self)
type(over), intent(out) :: other
type(over), intent(in) :: self
print *, "Assignment of over called"
other%ii = -1
end subroutine over_assign
!subroutine wrapper_assign(other, self)
! type(wrapper), intent(out) :: other
! type(wrapper), intent(in) :: self
!
! other%myover = self%myover
!
!end subroutine wrapper_assign
end module test_module
program test
use test_module
implicit none
type(wrapper) :: w1, w2
print *, "Assigning wrapper instances:"
w2 = w1
end program test
Run Code Online (Sandbox Code Playgroud)
这种[不幸]情况是语言规则(F90 +)对派生类型的内在分配的结果.细节在F2008 7.2.1p13中详细说明.总而言之,派生类型的内在赋值(使用特定注释掉的wrapper_assign发生的赋值)不会为派生类型的任何组件调用非类型绑定定义的赋值.在F90/F95中,如果要在组件层次结构的某个较低级别定义赋值,则需要为所有父组件定义分配,直到基础对象.
F2003为语言添加了类型绑定定义的赋值,这是由派生类型的内在赋值调用的.使用它代替指定已定义赋值的独立通用形式.(这也避免了类型名称可访问的潜在问题,但定义的分配过程无法访问.)