I wish to have a generic interface to routines that manipulate my program's
global data structures (dynamic memory which are stored in a module so are
accessable to all the other program's modules). I have structured these data
structures so that the components of the structures can be accessed either
through pointers or static structures (the code will be extended by others
so I'd prefer not to constrain them in how they access the data ...
depending on whether they are more comfortable with static structures or
pointers). This all works well until I try to define a generic interface,
where I try to distinguish on whether the subroutines are passed (1) a
pointer to the structure or (2) the static structure itself.
I have tried this with Intel Fortran V9 and G95 with the same results. The
program below executes fine as stands. When the module procedure
declarations are uncommented they are flagged as ambiguous despite the types
being different? Why?
Of course, despite the subroutines only being different in whether they are
passed a pointer or not, you cannot pass the pointer to the static version
or vice versa either (... Not knowing the internals of how the compilers
work I thought maybe the pass by reference could be identical in both cases).
module types
type type1
integer :: data1
real,dimension(:),pointer :: data3=>null()
end type type1
interface subtype1
! the following two statements when uncommented yield an 'ambiguous interface'
! module procedure allocatetype1
! module procedure allocatetype1ptr
end interface subtype1
contains
subroutine allocatetype1(var1)
implicit none
type(type1) :: var1
integer :: status
write (*,*) 'allocatetype1'
if (associated(var1%data3)) then
write (*,*) 'error for type1'
else
allocate(var1%data3(1:10),stat=status)
end if
end subroutine allocatetype1
subroutine allocatetype1ptr(varptr)
implicit none
type(type1),pointer :: varptr
integer :: status
write (*,*) 'allocatetype1ptr'
if (associated(varptr%data3)) then
write (*,*) 'error for type1ptr'
else
allocate(varptr%data3(1:10),stat=status)
end if
end subroutine allocatetype1ptr
end module types
program testsimple
USE types
implicit none
type(type1) :: var
type(type1),pointer :: var1=>null()
call allocatetype1(var)
! call subtype1(var)
allocate(var1)
call allocatetype1ptr(var1)
! call subtype1(var1)
read(*,*)
end program testsimple
|