- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi -
Does anyone have a suggestion about how to make generic routines in a module which can accept either an allocatable or a pointer as an argument?
At the moment I have resorted to writing a two routines, one for pointers and one for allocatables. The allocatable version makes pointers to the arguments and calls the pointer version, but I still can't use an interface statement because the type/kind/arugments all match!
So now for example I have
SUBROUTINE SETUP_INTERP_A(Xin,Yin,Xout,Yout,WEIGHTS)
IMPLICIT NONE
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Xin(:,:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Yin(:,:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Xout(:)
REAL(SP), ALLOCATABLE, TARGET, INTENT(IN) :: Yout(:)
TYPE(INTERP_WEIGHTS), INTENT(OUT) :: WEIGHTS
REAL(SP), POINTER :: XinP(:,:)
REAL(SP), POINTER :: YinP(:,:)
REAL(SP), POINTER :: XoutP(:)
REAL(SP), POINTER :: YoutP(:)
NULLIFY(XinP,YinP,XoutP,YoutP)
IF(ALLOCATED(Xin)) XinP => Xin
IF(ALLOCATED(Yin)) YinP => Yin
IF(ALLOCATED(Xout)) XoutP => Xout
IF(ALLOCATED(Yout)) YoutP => Yout
CALL SETUP_INTERP_P(XinP,YinP,XoutP,YoutP,WEIGHTS)
END SUBROUTINE SETUP_INTERP_A
SUBROUTINE SETUP_INTERP_P(Xin,Yin,Xout,Yout,WEIGHTS)
IMPLICIT NONE
REAL(SP), POINTER, INTENT(IN) :: Xin(:,:)
REAL(SP), POINTER, INTENT(IN) :: Yin(:,:)
REAL(SP), POINTER, INTENT(IN) :: Xout(:)
REAL(SP), POINTER, INTENT(IN) :: Yout(:)
TYPE(INTERP_WEIGHTS), INTENT(OUT) :: WEIGHTS
...
...
...
END SETUP_INTERP_P
Another fine point of this probelm: is it bad form to pass allocatable arrays which do not have the attribute 'target' to setup_interp_a ? It seems to work?
David
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Generics cannot be distinguished by anything other than Type, Kind and Rank.
Within the routine itself, nothing you do to the allocatables is a problem. In the caller, though, lack of TARGET might cause problems if you modify the arrays through the pointers.
Within the routine itself, nothing you do to the allocatables is a problem. In the caller, though, lack of TARGET might cause problems if you modify the arrays through the pointers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Using the pointer outside the routine has not caused a problem yet, but if this is not a safe method I will have to look for other options.
It is extremely useful for allowing pointers to data in defined types - which is probably also a bit sketchy...

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page