我有一个在通用过程(GetValue)下有两个绑定过程(GetAsScalar & GetAsList)的类型:
type, extends(TObject) :: TKeyword
character(len=:), allocatable :: fValue
contains
procedure, private :: GetAsScalar
procedure, private :: GetAsList
generic :: GetValue => &
GetAsScalar, &
GetAsList
end type TKeyword
例程签名如下:
subroutine GetAsScalar (this, value, status)
!Arguments-------------------------------------------------------------
class(TKeyword) :: this
class(*), target :: value
logical, optional :: status
!...
end subroutine GetAsScalar
subroutine GetAsList (this, value, status)
!Arguments-------------------------------------------------------------
class(TKeyword) :: this
class(*), pointer :: value(:)
logical, optional :: status
!...
end subroutine GetAsList
在内部,TKeyword 对象存储一个字符串。
如果我尝试按以下方式使用它(如下所示),则会出现编译错误:“此类型绑定的通用子程序调用没有匹配的特定子程序”
class(TKeyword), pointer :: key
class(*), pointer :: p(:)
allocate (integer::p(3))
!Code to read instantiate the TKeyword object
call key%GetValue(p, status)
select type (p)
type is (integer)
write (*,*) p
end select
如果我从通用关联中删除 GetASScalar 并将其公开,则以下代码将按预期工作:
class(TKeyword), pointer :: key
class(*), pointer :: p(:)
allocate (integer::p(3))
!Code to read instantiate the TKeyword object
call key%GetAsList(p, status)
select type (p)
type is (integer)
write (*,*) p
end select
当传递一个标量(整数、实数、字符等)时,调用 GetAsScalar 例程没有问题。
我想知道为什么会这样。我在这个“通用的东西”中遗漏了什么,使编译器无法在通用下识别我的子例程?有办法使这项工作吗?会与例行签名有关吗?
我正在使用英特尔 Fortran 15.0.1.148