fortran扩展了不同模块的类型

对于Fortran中的扩展类型,应该通过对不同模块中的类型扩展可见的私有组件.

使用gcc4.7和ifort时,以下代码会导致错误,因为bName同时包含初始类型和扩展名.但由于它是私有的,因此在不同模块的扩展中是不可访问的,即,如果你在bar_type中注释掉bName,你将得到一个私有的错误.

module foo
  type :: foo_type
    character(256),private :: bName = "foo"
  contains
    procedure :: pName => pName
  end type
contains
  subroutine pName(this)
    class(foo_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine
end module

module bar
  use foo, only : foo_type
  type,extends(foo_type) :: bar_type
    character(256),private :: bName = "bar"
  contains
    procedure :: pName => pName
  end type
contains

  subroutine pName(this)
    class(bar_type), intent(in) :: this
    print*,this%bName
  end subroutine
end module


program test
  use foo, only : foo_type
  use bar, only : bar_type
  type(foo_type) :: foo_inst
  type(bar_type) :: bar_inst

  call foo_inst%pName()
  call bar_inst%pName()
end program

如果bar_type包含在与foo_type相同的模块中,则可以从bar_type访问bName,即,以下代码将编译

module foo
  type :: foo_type
    character(256),private :: bName = "foo"
  contains
    procedure :: pName => pName
  end type

  type, extends(foo_type) :: baz_type
  contains
    procedure :: pName => pName_baz
  end type
contains
  subroutine pName_baz(this)
    class(baz_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine

  subroutine pName(this)
    class(foo_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine
end module

program test
  use foo, only : foo_type,baz_type
  type(foo_type) :: foo_inst
  type(baz_type) :: baz_inst

  call foo_inst%pName()
  call baz_inst%pName()
end program

解析标准时很难知道第一个例子中应该发生什么.

最佳答案 我相信第一个例子不符合标准.

即使private属性使组件bName在模块foo外部不可访问,它仍然由bar_type继承(可能相当无意义,因为没有任何东西可以用它完成,但这不是问题) – 参见f2003中的注释4.51:

Inaccessible components and bindings of the parent type are also inherited, but they remain inaccessible
in the extended type. Inaccessible entities occur if the type being extended is accessed via
use association and has a private entity.

因此bar_type有一个名为bName的继承组件,这使得用该名称添加另一个组件时出错(参见第16.2节中的范围和名称规则).

点赞