我希望编写一些可以处理不同类型的程序。我打算使用flibs中描述的"include"方法,具体内容请参见这里和这里。下面是一个简单的例子。
! -------------------------------------------------------------- !
module data_type
type ivalue
integer :: v
end type
type rvalue
real(8) :: v
end type
end module data_type
! -------------------------------------------------------------- !
module imod
use data_type, only: T => ivalue
include "template.f90"
end module imod
! -------------------------------------------------------------- !
module rmod
use data_type, only: T => rvalue
include "template.f90"
end module rmod
! -------------------------------------------------------------- !
module mod
use imod, only:
& ivalue => T,
& iprintme => printme
use rmod, only:
& rvalue => T,
& rprintme => printme
private
public :: ivalue, rvalue
public :: printme
interface printme
module procedure iprintme
module procedure rprintme
end interface printme
end module mod
! -------------------------------------------------------------- !
program hello
use mod
implicit none
type(ivalue) :: iv
type(rvalue) :: rv
iv%v=42
rv%v=3.14
call printme(iv)
call printme(rv)
end program hello
使用包含的文件:
contains
subroutine printme(a)
implicit none
type(T) :: a
print *,a
end subroutine printme
令我困扰的是它似乎只适用于派生类型,而不适用于内在类型。如果模块mod的用户想要在简单整数上使用printme例程,那么对于他来说将其封装在ivalue类型中并且无法执行是非常烦人的:
integer :: a=42
call printme(a)
有没有办法将这种方法扩展到内在类型,或者使用严格的f90/f95进行操作的另一种方法(我不想使用“传输”方法,因为它会复制数据)?
谢谢!