重载赋值的嵌套派生类型 [英] Nested derived type with overloaded assignment

查看:141
本文介绍了重载赋值的嵌套派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含其他派生类型( over )的派生类型( wrapper )。对于后者,赋值运算符已经超载。由于派生类型的分配是按照默认的组件方式进行的,所以我认为分配两个 wrapper 实例会调用的重载赋值而不是在某个时刻。但是,使用下面的程序,似乎并非如此。如果我还重载了 wrapper 的赋值,该赋值包含在的实例之间赋值的 explicit 赋值, code>(通过取消注释已注释的代码行)。为什么?我觉得它有点反直觉。有没有办法避免包装类型的重载?

  module test_module 
隐式无

type :: over
integer :: ii = 0
结束类型超过

类型::包装
类型(结束):: myover
结束类型包装

接口分配(=)
模块过程over_assign
!模块过程wrapper_assign
结束接口分配(=)

包含

子程序over_assign(other,self)
type(over),intent(out):: other
type(over),intent(in):: self
$ b $ print *,赋值过的被调用
其他%ii = -1

结束子程序over_assign

!子程序wrapper_assign(other,自己)
!类型(包装),intent(out):: other
!类型(包装),intent(in):: self

!其他%myover = self%myover

!end子程序wrapper_assign

结束模块test_module

程序测试
使用test_module
隐式无

类型(包装):: w1,w2

print *,分配包装实例:
w2 = w1

结束程序测试


F2003为语言添加了类型绑定定义的赋值,并且被派生类型的内在赋值调用。使用它而不是指定定义赋值的独立通用形式。 (这也避免了类型名称可访问的潜在问题,但定义的赋值过程不可访问。)


I have a derived type (wrapper) containing an other derived type (over). For the latter the assignment operator have been overloaded. As the assignment of derived types happens per default componentwise, I'd expect that assigning two instances of wrapper would invoke the overloaded assignment for over at some point. However, using the program below, it does not seem to be the case. The overloaded assignment is only invoked if I also overload the assignment for wrapper containing an explicit assignment between instances of over (by uncommenting the commented code lines). Why? I find it somewhat counter intuitive. Is there any way to avoid the overloading in the wrapping type?

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

解决方案

This [unfortunate] situation is a consequence of the rules of the language (F90+) for intrinsic assignment of derived types. The details are spelled out in F2008 7.2.1p13. As a summary, intrinsic assignment of derived types (the assignment that happens with the wrapper_assign specific commented out) does not invoke non-type bound defined assignment for any components that are of derived type. In F90/F95, if you want defined assignment at some lower level of the component hierarchy then you need to have defined assignment for all the parent components up to the base object.

F2003 added type bound defined assignment to the language and this is invoked by intrinsic assignment of derived types. Use that instead of the stand-alone generic form of specifying defined assignment. (This also avoids a potential problem with the type name being accessible but the defined assignment procedure not being accessible.)

这篇关于重载赋值的嵌套派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆